$ go run <filename>.go
Or
$ go build <filename>.go
$ ./<filename>
package main
func main() {
}
Output
import "fmt"
...
fmt.Println()
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)
}
import (
"fmt"
"time"
"math/rand"
)
func main() {
fmt.Println("The time is", time.Now())
fmt.Println("My favorite number is", rand.Intn(10))
}
exported names begin in capital letter
func add(x int, y int) int { //can ommit the type of x because it's the same as y
return x + y
}
hypot := func(x, y float64) float64 {
return math.Sqrt(x*x + y*y)
}
func swap(x, y string) (string, string) {
return y, x
}
use only in short functions
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
var can be use in package and function level
var c, python, java bool
var i, j int = 1, 2
the type can be ommited when initializing variable
var c, python, java = true, false, "no!"
inside a function, := can be used instead of var
k := 3
c, python, java := true, false, "no!"
factored variable declaration
var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
z complex128 = cmplx.Sqrt(-5 + 12i)
)
variables not initialized will be assigned their zero values 0 for numbers false for bool "" for string
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)
Or, put more simply:
i := 42
f := float64(i)
u := uint(f)
i := 42 // int
f := 3.142 // float64
g := 0.867 + 0.5i // complex128
fmt.Printf("i is of type %T\n", i)
const Pi = 3.14
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
// You can leave the pre and post statements empty
sum := 1
for ; sum < 1000; {
sum += sum
}
sum := 1
for sum < 1000 {
sum += sum
}
for {
}
if x < 0 {
return sqrt(-x) + "i"
}
//the short statement is executed before the condition
if v := math.Pow(x, n); v < lim {
return v
}
Short statement variables can only be accessed inside the if..else statement
if v := math.Pow(x, n); v < lim {
return v
} else {
fmt.Printf("%g >= %g\n", v, lim)
}
// the case breaks automatically. if you want it to continue use "fallthrough"
switch os := runtime.GOOS; os {
case "darwin":
fmt.Println("OS X.")
case "linux":
fmt.Println("Linux.")
default:
// freebsd, openbsd,
// plan9, windows...
fmt.Printf("%s.", os)
}
//switch with no condition is same as switch true
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("Good morning!")
case t.Hour() < 17:
fmt.Println("Good afternoon.")
default:
fmt.Println("Good evening.")
}
//defer executes the function after the surrounding function returns
defer fmt.Println("world")
When there is more than one deferred function calls they are executed in stack(last-in-first-out)
var p *int
Example:
p := &i // point to i
fmt.Println(*p) // read i through the pointer
*p = 21 // set i through the pointer
fmt.Println(i) // see the new value of i
type Vertex struct {
X int
Y int
}// can also be factored to X, Y int
...
v := Vertex{1, 2}
v.X = 4
fmt.Println(v.X)
v := Vertex{1, 2}
p := &v
p.X = 1e9
fmt.Println(v)
var (
v1 = Vertex{1, 2} // has type Vertex
v2 = Vertex{X: 1} // Y:0 is implicit
v3 = Vertex{} // X:0 and Y:0
p = &Vertex{1, 2} // has type *Vertex
)
var a [10]int
s := []int{2, 3, 5, 7, 11, 13}
s[1:4] == [3 5 7]
s[:3] == [2 3 5]
s[4:] == [11 13]
b := make([]int, 0, 5) // len(b)=0, cap(b)=5
var z []int // len=(z)=0, cap(z)=0
append(z, 0, 1, ...)
var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
...
for i, v := range pow {// i has index, v has the value
fmt.Printf("2**%d = %d\n", i, v)
}
you can assign the index or value to _ to skip it you can remove ', v' to get the index only
type Vertex struct {
Lat, Long float64
}
var m map[string]Vertex
...
m = make(map[string]Vertex)
m["Bell Labs"] = Vertex{
40.68433, -74.39967,
}
var m = map[string]Vertex{
"Bell Labs": Vertex{
40.68433, -74.39967,
},
"Google": Vertex{
37.42202, -122.08408,
},
}
or simply
var m = map[string]Vertex{
"Bell Labs": {40.68433, -74.39967},
"Google": {37.42202, -122.08408},
}
Insert or update an element in map m:
m[key] = elem
Retrieve an element:
elem = m[key]
Delete an element:
delete(m, key)
Test that a key is present with a two-value assignment:
elem, ok = m[key]
func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}
func main() {
pos, neg := adder(), adder()
for i := 0; i < 10; i++ {
fmt.Println(
pos(i),
neg(-2*i),
)
}
}
type Vertex struct {
X, Y float64
}
func (v *Vertex) Scale(f float64) {
v.X = v.X * f
v.Y = v.Y * f
}
func (v *Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func main() {
v := &Vertex{3, 4}
fmt.Println(v.Abs())
}
type MyFloat float64
func (f MyFloat) Abs() float64 {
if f < 0 {
return float64(-f)
}
return float64(f)
}
type Abser interface {
Abs() float64
}
func main() {
var a Abser
f := MyFloat(-math.Sqrt2)
v := Vertex{3, 4}
a = f // a MyFloat implements Abser
a = &v // a *Vertex implements Abser
// In the following line, v is a Vertex (not *Vertex)
// and does NOT implement Abser.
a = &v
fmt.Println(a.Abs())
}
type MyFloat float64
func (f MyFloat) Abs() float64 {
if f < 0 {
return float64(-f)
}
return float64(f)
}
type Vertex struct {
X, Y float64
}
func (v *Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
type Person struct {
Name string
Age int
}
func (p Person) String() string {
return fmt.Sprintf("%v (%v years)", p.Name, p.Age)
}
type MyError struct {
When time.Time
What string
}
func (e *MyError) Error() string {
return fmt.Sprintf("at %v, %s",
e.When, e.What)
}
func run() error {
return &MyError{
time.Now(),
"it didn't work",
}
}
func main() {
if err := run(); err != nil {
fmt.Println(err)
}
}
func main() {
r := strings.NewReader("Hello, Reader!")
b := make([]byte, 8)
for {
n, err := r.Read(b)
fmt.Printf("n = %v err = %v b = %v\n", n, err, b)
fmt.Printf("b[:n] = %q\n", b[:n])
if err == io.EOF {
break
}
}
}
package main
import (
"fmt"
"log"
"net/http"
)
type Hello struct{}
func (h Hello) ServeHTTP(
w http.ResponseWriter,
r *http.Request) {
fmt.Fprint(w, "Hello!")
}
func main() {
var h Hello
err := http.ListenAndServe("localhost:4000", h)
if err != nil {
log.Fatal(err)
}
}
http.Handle("/string", String("I'm a frayed knot."))
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
func sum(a []int, c chan int) {
sum := 0
for _, v := range a {
sum += v
}
c <- sum // send sum to c
}
func main() {
a := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(a[:len(a)/2], c)
go sum(a[len(a)/2:], c)
x, y := <-c, <-c // receive from c
fmt.Println(x, y, x+y)
}
func main() {
ch := make(chan int, 2)
ch <- 1
ch <- 2
fmt.Println(<-ch)
fmt.Println(<-ch)
}