Install
https://stackoverflow.com/questions/12843063/install-go-with-brew-and-running-the-gotour
https://golang.org/doc/install#install
Test
cd $HOME/go/src/hello
go build
1 2 3 4 5 6 7 |
package main import "fmt" func main() { fmt.Printf("hello, world\n") } |
./hello
Declaration
Basic
1 2 3 4 5 6 7 8 9 10 11 12 |
package main import "fmt" func main() { var message string var a, b, c int = 1, 2, 3 message = "Hello all" fmt.Printf(message, a, b, c) } |
and more idiomatically:
1 2 3 4 5 6 7 8 9 10 11 12 |
package main import "fmt" func main() { message := string a, b, c := 1, 2, 3 message = "Hello all" fmt.Printf(message, a, b, c) } |
Pointers
1 2 3 |
var greeting *string = &message fmt.Printf(message, greeting) |
*string => pointer to a string
&message => use the memory location of the variable message
so this returns
Hello all%!(EXTRA *string=0xc42007a1c0)
whereas this:
1 |
fmt.Printf(message, *greeting) |
returns the value. i.e.
Hello all%!(EXTRA string=Hello all)
Basic User Types
type Salutation string
or a more complex example:
type Salutation struct {
name string
greeting string
}
func main() {
var a = Salutation{name: “Bob”, greeting: “Hi” }
fmt.Println(s.greeting, s.name)
}
Getters / Setters and Scope
There are no Getters / Setters.
A capitalised variable is publicly visible from a package. E.g. Salutation.
Lowercase is not visible.
Constants
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package main import "fmt" type Salutation struct { name string greeting string } const ( PI = 3.14 Language = "Go" A = iota B = iota C = iota ) func main() { var s = Salutation{name: "Bob", greeting: "Hi" } fmt.Println(s.greeting, s.name) fmt.Println(PI, Language, A, B, C) } |
Outputs:
1 2 |
Hi Bob 3.14 Go 2 3 4 |
Note on iota. This is used in const
delarations to increment numbers.
https://github.com/golang/go/wiki/Iota
A better example of using iota
is probably this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package main import "fmt" type Salutation struct { name string greeting string } const ( PI = 3.14 Language = "Go" ) type Weekday int const ( Sunday Weekday = iota + 1 Monday Tuesday Wednesday Thursday Friday Saturday ) func main() { var s = Salutation{name: "Bob", greeting: "Hi" } fmt.Println(s.greeting, s.name) fmt.Println(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday) } |
Note the implicit iota
in the const for subsequent Weekdays.
Functions
- Multiple return values
- Use like any other type
- Function literals
Note: function literals aka lambdas or closures are functions without a name.
https://programming.guide/go/anonymous-function-literal-lambda-closure.html
E.g. of function declaration:
package main import "fmt" type Salutation struct { name string greeting string } func Greet(salutation Salutation) { fmt.Println(salutation.greeting) fmt.Println(salutation.name) } func main() { var s = Salutation{name: "Bob", greeting: "Hi" } Greet(s) }
Order of type and variable name
Note the type
comes after the variable name (i.e. opposite to C).
E.g.
1 2 3 |
func add(x int, y int) int { return x + y } |
This is different to C
. For why see https://blog.golang.org/gos-declaration-syntax
Also, if function parameters share a type you can leave the type to the end.
E.g.
1 2 3 |
func add(x, y int) int { return x + y } |
Multiple return values
E.g.
1 2 3 |
func swap(x, y string) (string, string) { return y, x } |
Exported names
Names are exported if they begin with a capital letter.
i.e. Pi
would be exported but pi
would not.
Variable declarations
Multiple declarations:
var i, j int = 1, 2
Here k
has an implicit type of int
.
k := 3
Zero values:
0
for numeric
false
for Boolean
""
for strings
Type conversions
E.g.
i := 42
f := float64(i)
u := uint(f)
REPL
Handy online REPL here: