// hello.go package main import "fmt" func main() { fmt.Println("Hello Go") }
go run hello.go
var foo int // declaration without initialization var foo int = 42 // declaration with initialization var foo, bar int = 42, 1302 // declare and init multiple vars at once var foo = 42 // type omitted, will be inferred foo := 42 // shorthand, only in func bodies, omit var keyword, type is always implicit const constant = "This is a constant"
// a simple function func functionName() {} // function with parameters (again, types go after identifiers) func functionName(param1 string, param2 int) {} // multiple parameters of the same type func functionName(param1, param2 int) {} // return type declaration func functionName() int { return 42 } // Can return multiple values at once func returnMulti() (int, string) { return 42, "foobar" } var x, str = returnMulti() // Return multiple named results simply by return func returnMulti2() (n int, s string) { n = 42 s = "foobar" // n and s will be returned return } var x, str = returnMulti2()
func main() { // assign a function to a name add := func(a, b int) int { return a + b } // use the name to call the function fmt.Println(add(3, 4)) } // Closures, lexically scoped: Functions can access values that were // in scope when defining the function func scope() func() int { outer_var := 2 foo := func() int { return outer_var} return foo } // Closures func outer() (func() int, int) { outer_var := 2 inner := func() int { outer_var += 99 // outer_var from outer scope is mutated. return outer_var } inner() return inner, outer_var // return inner func and mutated outer_var 101 }