From: acevest Date: Sun, 4 Jan 2015 11:58:09 +0000 (+0800) Subject: ... X-Git-Url: http://zhaoyanbai.com/repos/?a=commitdiff_plain;h=932dbec61739d0d38f68991c5fd5e7b0c2bf7d1b;p=acecode.git ... --- diff --git a/learn/go/hello/array.go b/learn/go/hello/array.go new file mode 100644 index 0000000..11e9841 --- /dev/null +++ b/learn/go/hello/array.go @@ -0,0 +1,29 @@ +package main + +import "fmt" + +func main() { + var array [2]string + array[0] = "HELLO" + array[1] = "GO" + + fmt.Println(array[0], array[1]) + fmt.Println(array) + + var a int = 1 + fmt.Println(a) + b := [2]string{"haha", "hehe"} + var c [2]string = [2]string{"a", "b"} + // ERROR: var c [2]string = {"a", "b"} + fmt.Println(b) + fmt.Println(c) + + // SLICE + d := []byte{'a', 'b', 'c'} + fmt.Println(d) + var e []string = []string{"a", "b", "c"} + + for i := 0; i < len(e); i++ { + fmt.Println(e[i]) + } +} diff --git a/learn/go/hello/hello.go b/learn/go/hello/hello.go index 49c33d0..6e6f83d 100644 --- a/learn/go/hello/hello.go +++ b/learn/go/hello/hello.go @@ -4,9 +4,31 @@ import "fmt" import "math/rand" import "time" +func GetStr() (string, string) { + const stra = "Hello" + const strb string = "World" + return stra, strb +} + +func PrintType() { + var a int + var b int32 + var c int64 + var d uint32 + var e float32 + var f bool + var g complex64 + var h complex128 + + fmt.Printf("%T %T %T %T %T %T %T %T\n", a, b, c, d, e, f, g, h) +} + func main() { + defer fmt.Println("----------------") rand.Seed(time.Now().UnixNano()) - for i := 0; i < rand.Intn(10); i++ { - fmt.Printf("Hello World\n") + for i := 0; i < rand.Intn(10)+1; i++ { + fmt.Println(GetStr()) } + + PrintType() } diff --git a/learn/go/hello/struct.go b/learn/go/hello/struct.go new file mode 100644 index 0000000..b6bbc7d --- /dev/null +++ b/learn/go/hello/struct.go @@ -0,0 +1,17 @@ +package main + +import "fmt" + +type Vertex struct { + X int + Y int +} + +func main() { + v := Vertex{1, 2} + fmt.Println(v) + + p := &v + p.X = 2 + fmt.Println(v) +} diff --git a/learn/go/hello/switch.go b/learn/go/hello/switch.go new file mode 100644 index 0000000..8a10f04 --- /dev/null +++ b/learn/go/hello/switch.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "runtime" + "time" +) + +func main() { + fmt.Print("Go runs on ") + 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) + } + + 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.") + } + +}