func main() { fmt.Println("### Start Slice Exercise ###") pic.Show(Pic) fmt.Println("### End Slice Exercise ###") }
func main() { pointer() v := Vertex{1, 2} v.X = 4 fmt.Println(v) p := &v p.X = 1e9 fmt.Println(v) structLiterals() array() slice() tictactoe() slicing() makeslice() zeroSlice() appendSlice() ranges() pic.Show(Pic) var vLL = VertexLL{40.68433, -74.39967} geography("Bell Labs", vLL) fmt.Println(m["Bell Labs"]) vLL = VertexLL{37.4828, 122.2361} geography("Redwood City", vLL) fmt.Println(m["Redwood City"]) fmt.Println(m) mutateMap() wc.Test(WordCount) fmt.Println(hypot(5, 12)) fmt.Println(compute(hypot)) fmt.Println(compute(math.Pow)) exAdder() f := fibonacci() for i := 0; i < 10; i++ { fmt.Println(f()) } }
func main() { pic.Show(Pic) }
func TestExerciseSlices(t *testing.T) { pic.Show(Pic) }
func main() { i, j := 42, 2701 // pointer to i p := &i fmt.Println(p, " -> ", *p) p = &j fmt.Println(p, " -> ", *p) *p = i fmt.Println(p, " -> ", *p) v1 := Vertex{4, 5} v2 := Vertex{X: 9, Y: 9} v3 := Vertex{X: 9} // Y=0 is implicit fmt.Println(v1.X) fmt.Println(v2, v3) // arrays var s1 [2]string s1[0] = "hi" s1[1] = "there" fmt.Println("a[0]: ", s1[0]) fmt.Println("a: ", s1) arr := []int{1, 2, 4, 5, 23, 123, 5, 0} fmt.Println("arr ==", arr) for i := 0; i < len(arr); i++ { fmt.Printf("arr[%d] == %d\n", i, arr[i]) } fmt.Println("arr[:3] == ", arr[:3]) fmt.Println("arr[2:5] == ", arr[2:5]) fmt.Println("arr[5:] == ", arr[5:]) fmt.Println("arr[:] == ", arr[:]) a := make([]int, 5) printSlice("a", a) b := make([]int, 0, 5) printSlice("b", b) c := b[:2] printSlice("c", c) d := c[2:5] printSlice("d", d) var z []int // zero value for empty array is nil if z == nil { fmt.Println("nil,", z) } printSlice("z", z) z = append(z, 1) z = append(z, 2) z = append(z, 4) var z2 []int = append(z, 3, 5, 6) printSlice("z", z) printSlice("z", z2) for i, v := range pow { fmt.Printf("2^%d = %d\n", i, v) } pwrs := make([]int, 10) for i := range pwrs { pwrs[i] = 1 << uint(i) } for _, val := range pwrs { fmt.Println(val) } pic.Show(Pic) var m map[string]Vertex m = make(map[string]Vertex) m["Bell labs"] = Vertex{342, 4532} var m2 = map[string]Vertex{ "Google": {33, 44}, "General Electric": {54322, 3423}, "deleteme": {}, } delete(m2, "deleteme") fmt.Println(m, m2) // functions hypnot := func(x, y float64) float64 { return math.Sqrt(x*x + y*y) } fmt.Println(hypnot(4.0, 3.0)) adder := closureAdder() for i := 0; i < 10; i++ { fmt.Printf(" %d ", adder(i)) } fmt.Printf("\n") }
////Learning material used from //https://tour.golang.org/moretypes/15 //GoTour needs to be installed locally func main() { rand.Seed(time.Now().UTC().UnixNano()) pic.Show(Pic) }
func main() { pic.Show(Pic) fmt.Println("hello, world") }
func showPic() { pic.Show(Pic) }
func main() { //pic.Show(Pic(2,2)) pic.Show(Pic) }
func main() { fmt.Println("Welcome to the playground!") fmt.Println("The time is", time.Now()) fmt.Println("My favorite number is", rand.Intn(10)) fmt.Printf("Now you have %g problems.", math.Nextafter(2, 3)) fmt.Println(math.Pi) fmt.Println(add1(42, 13)) fmt.Println(add2(42, 13)) a, b := swap("hello", "world") fmt.Println(a, b) fmt.Println(split(17)) //1 var i1 int fmt.Println(i1, c1, python1, java1) //2 var c2, python2, java2 = true, false, "no!" fmt.Println(i2, j2, c2, python2, java2) //3 var i3, j3 int = 1, 2 k3 := 3 c3, python3, java3 := true, false, "no!" fmt.Println(i3, j3, k3, c3, python3, java3) const f4 = "%T(%v)\n" fmt.Printf(f4, ToBe, ToBe) fmt.Printf(f4, MaxInt, MaxInt) fmt.Printf(f4, z4, z4) // data types // bool // string // int int8 int16 int32 int64 // uint uint8 uint16 uint32 uint64 uintptr // byte // alias for uint8 // rune // alias for int32 // // represents a Unicode code point // float32 float64 // complex64 complex128 // default value var i5 int var f5 float64 var b5 bool var s5 string fmt.Printf("%v %v %v %q\n", i5, f5, b5, s5) var x6, y6 int = 3, 4 var f6 float64 = math.Sqrt(float64(x6*x6 + y6*y6)) var z6 int = int(f6) fmt.Println(x6, y6, z6) v2 := 1.3 // change me! fmt.Printf("v is of type %T\n", v2) const World = "世界" fmt.Println("Hello", World) fmt.Println("Happy", Pi, "Day") const Truth = true fmt.Println("Go rules?", Truth) fmt.Println(needInt(Small)) fmt.Println(needFloat(Small)) fmt.Println(needFloat(Big)) sum1 := 0 for i := 0; i < 10; i++ { sum1 += i } fmt.Println(sum1) sum2 := 1 for sum2 < 1000 { sum2 += sum2 } fmt.Println(sum2) // For is Go's "while" sum3 := 1 for sum3 < 1000 { sum3 += sum3 } fmt.Println(sum3) fmt.Println(sqrt(2), sqrt(-4)) fmt.Println( pow1(3, 2, 10), pow1(3, 3, 20), ) //switch 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) } fmt.Println("When's Saturday?") today := time.Now().Weekday() fmt.Println(today, time.Saturday) switch time.Saturday { case today + 0: fmt.Println("Today.") case today + 1: fmt.Println("Tomorrow.") case today + 2: fmt.Println("In two days.") default: fmt.Println("Too far away.") } 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.") } DeferFunc() // end of flow control statements // pointers fmt.Println("Pointers ---") i, j := 42, 2701 var p *int // declaring a pointer to an int. p = &i // point to i fmt.Println(*p) // read i through the pointer. This is known as "dereferencing" or "indirecting". *p = 21 // set i through the pointer. This is known as "dereferencing" or "indirecting". fmt.Println(i) // see the new value of i p = &j // point to j. *p = *p / 37 // divide j through the pointer. This is known as "dereferencing" or "indirecting". fmt.Println(j) // see the new value of j fmt.Println("Pointers ---") // Structs fmt.Println(Vertex{1, 2}) v1 := Vertex{1, 2} v1.X = 4 fmt.Println(v1.X) p1 := &v1 p1.X = 1e9 fmt.Println(v1) fmt.Println(v3, p2, v4, v5) //arrays var array [2]string array[0] = "Hello" array[1] = "World" fmt.Println(array[0], array[1]) fmt.Println(array) s := []int{2, 3, 5, 7, 11, 13} fmt.Println("s ==", s) for i := 0; i < len(s); i++ { fmt.Printf("s[%d] == %d\n", i, s[i]) } // slicing fmt.Println("s[1:4] ==", s[1:4]) // missing low index implies 0 fmt.Println("s[:3] ==", s[:3]) // missing high index implies len(s) fmt.Println("s[4:] ==", s[4:]) a1 := make([]int, 5) printSlice("a1", a1) b1 := make([]int, 0, 5) printSlice("b1", b1) c1 := b1[:2] printSlice("c1", c1) d1 := c1[2:5] printSlice("d1", d1) //Nil slices var zNil []int fmt.Println(zNil, len(zNil), cap(zNil)) if zNil == nil { fmt.Println("nil!") } var a2 []int printSlice("a2", a2) // append works on nil slices. a2 = append(a2, 0) printSlice("a2", a2) // the slice grows as needed. a2 = append(a2, 1) printSlice("a2", a2) // we can add more than one element at a time. a2 = append(a2, 2, 3, 4) printSlice("a2", a2) // range for i, v := range pow2 { fmt.Printf("2**%d = %d\n", i, v) } pow3 := make([]int, 10) for i := range pow3 { pow3[i] = 1 << uint(i) } for _, value := range pow3 { fmt.Printf("%d\n", value) } pic.Show(Pic) // Maps map1 = make(map[string]LocationCoordinate) map1["Bell Labs"] = LocationCoordinate{ 40.68433, -74.39967, } fmt.Println(map1["Bell Labs"]) fmt.Println(map1) fmt.Println(map2) //Mutating Maps map3 := make(map[string]int) map3["Answer"] = 42 fmt.Println("The value:", map3["Answer"]) v6, ok1 := map3["Answer"] fmt.Println("The value:", v6, "Present?", ok1) map3["Answer"] = 48 fmt.Println("The value:", map3["Answer"]) delete(map3, "Answer") fmt.Println("The value:", map3["Answer"]) v6, ok2 := map3["Answer"] fmt.Println("The value:", v6, "Present?", ok2) // map exercise wc.Test(WordCount) //functions arevalues too hypot := func(x, y float64) float64 { return math.Sqrt(x*x + y*y) } fmt.Println(hypot(3, 4)) pos, neg := adder(), adder() for i := 0; i < 10; i++ { fmt.Println( pos(i), neg(-2*i), ) } fib := fibonacci() for i := 0; i < 10; i++ { fmt.Println(fib()) } v7 := &FloatVertex{3, 4} fmt.Println("FloatVertex", v7.Abs()) f1 := MyFloat(-math.Sqrt2) fmt.Println(f1.Abs()) v8 := &FloatVertex{3, 4} v8.Scale(5) fmt.Println(v8, v8.Abs()) runInterface() runImplicitInterface() runStringer() runErrors() go say("world") say("hello") runGoRoutine() runBufferedChannel() runRangeAndClose() runFibonacci3() runDefaultSelection() }
func main() { pic.Show(Pic) // base64 }
func main() { pic.Show(Pic) pic.Show(PicRange) }
func testPic() { pic.Show(Pic) }
// 参考 https://tour.go-zh.org/moretypes/15 func main() { // nice choice (x+y)/2、x*y 和 x^y pic.Show(Pic) }
func ExecPic() { pic.Show(Pic) }