[tags]:- go, learning, programming, note, WIP
This post is my notes as I read and work through the "tour of go" interactive introduction to golang.
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, 世界")
}
main
package main
import (
"fmt"
"math/rand" // "math/rand" -> rand
)
func main() {
fmt.Println("My favorite number is", rand.Int(10))
}
package main
import (
"fmt"
"math"
)
/*
Can also be written as individual line items, the parenthesized "factored" version is preferred
import "fmt"
import "math"
*/
func main() {
fmt.Printf("Now you have %g problems.\n", math.Sqrt(7))
}
package main
import "fmt"
func add(x int, y int) int {
return x + y
}
func main(){
fmt.Println(add(42, 13))
}
package main
import "fmt"
func add(x, y int) int {
return x + y
}
func main(){
fmt.Println(add(42, 13))
}
package main
import "fmt"
func swap(x, y string) (string, string) {
return y, x // multiple results
}
func main() {
a, b := swap("hello", "world") // grabbing both results
fmt.Println(a, b)
}
package main
import "fmt"
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return // return (x, y)
}
func main() {
fmt.Println(split(17)) // prints 7 10
}
var
declares a list of variables.var
statements can be at the package or function levelpackage main
import "fmt"
var c, python, java bool // package level
func main() {
var i int // function level
fmt.Println(i, c, python, java)
}
var
declaration can include initializers, one per variablepackage main
import "fmt"
var i, j int = 1, 2
func main() {
var c, python, java = true, false, "no!"
fmt.Println(i, j, c, python, java)
}
:=
can be used instead of a var
declaration with implicit typingpackage main
import "fmt"
func main() {
var i, j int = 1, 2
k := 3
c, python, java := true, false, "no!"
fmt.Println(i, j, k, c, python, java)
}
package main
import (
"fmt"
"math/cmplx"
)
var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
z complex128 = cmplx.Sqrt(-5 + 12i)
)
func main() {
fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
fmt.Printf("Type: %T Value: %v\n", z, z)
}
0
for numeric typesfalse
for booleans""
(empty string) for stringspackage main
import "fmt"
func main() {
var i int
var f float64
var b bool
var s string
fmt.Printf("%v %v %v %q\n", i, f, b, s)
}
T(v)
converts the value v
to the type T
package main
import (
"fmt"
"math"
)
func main() {
var x, y int = 3, 4
var f float64 = math.Sqrt(float64(x*x + y*y)) // long style
z := uint(f) // short style
fmt.Println(x, y, z)
}
// Typed example
var i int
j := i // j is an int
// Untyped example
i := 42 // int
f := 3.142 // float64
g := 0.867 + 0.5i // complex128
package main
import "fmt"
func main() {
v := 42 // change me!
fmt.Printf("v is of type %T\n", v)
}
const
keyword:=
syntaxpackage main
import "fmt"
const Pi = 3.14
func main() {
const World = "世界"
fmt.Println("Hello", World)
fmt.Println("Happy", Pi, "Day")
const Truth = true
fmt.Println("Go rules?", Truth)
}
package main
import "fmt"
const (
// Create a huge number by shifting a 1 bit left 100 places.
// In other words, the binary number that is 1 followed by 100 zeroes.
Big = 1 << 100
// Shift it right again 99 places, so we end up with 1<<1, or 2.
Small = Big >> 99
)
func needInt(x int) int { return x*10 + 1 }
func needFloat(x float64) float64 {
return x * 0.1
}
func main() {
fmt.Println(needInt(Small))
//fmt.Println(needInt(Big)) will cause crash (an int can store at max a 64-bit integer)
fmt.Println(needFloat(Small))
fmt.Println(needFloat(Big))
}
for
loopfor
loop has three components seperated by semicolonsfor
statement and the braces {}
are always requiredpackage main
import "fmt"
func main() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}