]> Zhao Yanbai Git Server - acecode.git/commitdiff
...
authoracevest <zhaoyanbai@126.com>
Sun, 4 Jan 2015 11:58:09 +0000 (19:58 +0800)
committeracevest <zhaoyanbai@126.com>
Sun, 4 Jan 2015 11:58:09 +0000 (19:58 +0800)
learn/go/hello/array.go [new file with mode: 0644]
learn/go/hello/hello.go
learn/go/hello/struct.go [new file with mode: 0644]
learn/go/hello/switch.go [new file with mode: 0644]

diff --git a/learn/go/hello/array.go b/learn/go/hello/array.go
new file mode 100644 (file)
index 0000000..11e9841
--- /dev/null
@@ -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])
+       }
+}
index 49c33d0969542247d1edcfc13e092ae10987a87c..6e6f83db57ec755514add555ffc14e191693a2de 100644 (file)
@@ -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 (file)
index 0000000..b6bbc7d
--- /dev/null
@@ -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 (file)
index 0000000..8a10f04
--- /dev/null
@@ -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.")
+       }
+
+}