The function value and parameters are evaluated as usual in the calling goroutine, but unlike with a regular call, program execution does not wait for the invoked function to complete. Instead, the function begins executing independently in a new goroutine. When the function terminates, its goroutine also terminates. If the function has any return values, they are discarded when the function completes.

go Server()
go func(ch chan<- bool) { for { sleep(10); ch <- true }} (c)
A "go" statement starts the execution of a function call as an independent concurrent thread of control, or goroutine, within the same address space.

GoStmt = "go" Expression .
If non-empty, the init statement is executed once before evaluating the condition for the first iteration; the post statement is executed after each execution of the block (and only if the block was executed). Any element of the ForClause may be empty but the semicolons are required unless there is only a condition. If the condition is absent, it is equivalent to the boolean value true.

for cond { S() }    is the same as    for ; cond ; { S() }
for      { S() }    is the same as    for true     { S() }
A labeled statement may be the target of a goto, break or continue statement.

LabeledStmt = Label ":" Statement .
Label       = identifier .
Implementation restriction: A compiler may disallow an empty expression list in a "return" statement if a different entity (constant, type, or variable) with the same name as a result parameter is in scope at the place of the return.

func f(n int) (res int, err error) {
	if _, err := f(n-1); err != nil {
		return  // invalid return statement: err is shadowed
	}
	return
}
The following assignment statements are semantically equivalent:

IncDec statement    Assignment
x++                 x += 1
x--                 x -= 1
For instance, if the deferred function is a function literal and the surrounding function has named result parameters that are in scope within the literal, the deferred function may access and modify the result parameters before they are returned. If the deferred function has any return values, they are discarded when the function completes. (See also the section on handling panics.)

lock(l)
defer unlock(l)  // unlocking happens before surrounding function returns

// prints 3 2 1 0 before surrounding function returns
for i := 0; i <= 3; i++ {
	defer fmt.Print(i)
}

// f returns 42
func f() (result int) {
	defer func() {
		// result is accessed after it was set to 6 by the return statement
		result *= 7
	}()
	return 6
}
Given an expression x of type interface{}, the following type switch:

switch i := x.(type) {
case nil:
	printString("x is nil")                // type of i is type of x (interface{})
case int:
	printInt(i)                            // type of i is int
case float64:
	printFloat64(i)                        // type of i is float64
case func(int) float64:
	printFunction(i)                       // type of i is func(int) float64
case bool, string:
	printString("type is bool or string")  // type of i is type of x (interface{})
default:
	printString("don't know the type")     // type of i is type of x (interface{})
}
With the exception of specific built-in functions, function and method calls and receive operations can appear in statement context. Such statements may be parenthesized.

ExpressionStmt = Expression .
A "for" statement with a ForClause is also controlled by its condition, but additionally it may specify an init and a post statement, such as an assignment, an increment or decrement statement. The init statement may be a short variable declaration, but the post statement must not. Variables declared by the init statement are re-used in each iteration.

ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .
InitStmt = SimpleStmt .
PostStmt = SimpleStmt .

Recommend

The Go Programming Language Specification Statements Go statements

The Go Programming Language Specification Statements For statements For statements with range clause

The Go Programming Language Specification Statements For statements For statements with for clause

The Go Programming Language Specification Statements For statements For statements with single condition

The Go Programming Language Specification Statements For statements

The Go Programming Language Specification Statements Switch statements Type switches

The Go Programming Language Specification Statements Switch statements Expression switches

The Go Programming Language Specification Statements Switch statements

The Go Programming Language Specification Statements If statements

The Go Programming Language Specification Statements Assignments

The Go Programming Language Specification Statements IncDec statements

The Go Programming Language Specification Statements Send statements

The Go Programming Language Specification Statements Expression statements

The Go Programming Language Specification Statements Labeled statements

The Go Programming Language Specification Statements Empty statements

The Go Programming Language Specification Statements

The Go Programming Language Specification Expressions Order of evaluation

The Go Programming Language Specification Expressions Constant expressions

The Go Programming Language Specification Expressions Conversions Conversions from slice to array pointer

The Go Programming Language Specification Expressions Conversions Conversions to and from a string type

The Go Programming Language Specification Expressions Conversions

The Go Programming Language Specification Expressions Receive operator

The Go Programming Language Specification Expressions Address operators

The Go Programming Language Specification Expressions Logical operators

The Go Programming Language Specification Expressions Comparison operators

The Go Programming Language Specification Expressions Arithmetic operators String concatenation

The Go Programming Language Specification Expressions Arithmetic operators Floating-point operators

The Go Programming Language Specification Expressions Arithmetic operators Integer operators

The Go Programming Language Specification Expressions Arithmetic operators

The Go Programming Language Specification Expressions Operators Operator precedence

The Go Programming Language Specification Expressions Operators

The Go Programming Language Specification Expressions Type inference Constraint type inference

The Go Programming Language Specification Expressions Type inference Function argument type inference

The Go Programming Language Specification Expressions Type inference Type unification

The Go Programming Language Specification Expressions Instantiations

The Go Programming Language Specification Expressions Passing arguments to ... parameters

The Go Programming Language Specification Expressions Calls

The Go Programming Language Specification Expressions Type assertions

The Go Programming Language Specification Expressions Slice expressions Full slice expressions

The Go Programming Language Specification Expressions Slice expressions Simple slice expressions

The Go Programming Language Specification Expressions Index expressions

The Go Programming Language Specification Expressions Method values