zoukankan      html  css  js  c++  java
  • Go语言学习笔记(2)

    数组,切片

    var a [2]string
    a[0] = "Hello"
    a[1] = "World"
    primes := [6]int{2, 3, 5, 7, 11, 13}
    // 切片(半开区间)
    var s []int = primes[1:4]
    names := [4]string{
        "John",
        "Paul",
        "George",
        "Ringo",
    }
    a := names[0:2] // [John Paul]
    b := names[1:3] // [Paul George]
    b[0] = "XXX" // a == [John XXX]  b == [XXX George]
    // 切片字面量
    q := []int{2, 3, 5, 7, 11, 13}
    r := []bool{true, false, true, true, false, true}
    s := []struct {
        i int
        b bool
    }{
        {2, true},
        {3, false},
        {5, true},
        {7, true},
        {11, false},
        {13, true},
    }
    // 省略上下界
    s := []int{2, 3, 5, 7, 11, 13}
    s = s[1:4] // [3 5 7]
    s = s[:2] // [3 5]
    s = s[1:] // [5]
    // 长度和容量
    func printSlice(s []int) {
        fmt.Printf("len=%d cap=%d %v
    ", len(s), cap(s), s)
    }
    s := []int{2, 3, 5, 7, 11, 13}
    printSlice(s) // len=6 cap=6 [2 3 5 7 11 13]
    s = s[:0]
    printSlice(s) // len=0 cap=6 []
    s = s[:4]
    printSlice(s) // len=4 cap=6 [2 3 5 7]
    s = s[2:]
    printSlice(s) // len=2 cap=4 [5 7]
    // 空切片
    var s []int
    fmt.Println(s, len(s), cap(s)) // [] 0 0
    if s == nil { // true
    }
    // make 函数
    a := make([]int, 5)
    printSlice("a", a) // a len=5 cap=5 [0 0 0 0 0]
    b := make([]int, 0, 5)
    printSlice("b", b) // b len=0 cap=5 []
    // 切片的切片
    board := [][]string{
        []string{"_", "_", "_"},
        []string{"_", "_", "_"},
        []string{"_", "_", "_"},
    }
    board[0][0] = "X"
    board[2][2] = "O"
    // append 函数
    var s []int
    printSlice(s) // len=0 cap=0 []
    s = append(s, 0)
    printSlice(s) // len=1 cap=1 [0]
    s = append(s, 1)
    printSlice(s) // len=2 cap=2 [0 1]
    s = append(s, 2, 3, 4)
    printSlice(s) // len=5 cap=6 [0 1 2 3 4]
    

    区间

    var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
    for i, v := range pow {
        fmt.Printf("2**%d = %d
    ", i, v)
    }
    /*
    2**0 = 1
    2**1 = 2
    ...
    2**7 = 128
    */
    for i, _ := range pow
    for _, value := range pow
    

    Map

    type Vertex struct {
        Lat, Long float64
    }
    var m map[string]Vertex
    m = make(map[string]Vertex)
    m["Bell Labs"] = Vertex{
        40.68433, -74.39967,
    }
    fmt.Println(m["Bell Labs"]) // {40.68433 -74.39967}
    // Map 字面量
    var m = map[string]Vertex{
        "Bell Labs": Vertex{
            40.68433, -74.39967,
        },
        "Google": Vertex{
            37.42202, -122.08408,
        },
    }
    // Map 字面量
    var m = map[string]Vertex{
        "Bell Labs": {40.68433, -74.39967},
        "Google":    {37.42202, -122.08408},
    }
    // Map 应用
    m := make(map[string]int)
    m["Answer"] = 42
    fmt.Println("The value:", m["Answer"]) // The value: 42
    m["Answer"] = 48
    fmt.Println("The value:", m["Answer"]) // The value: 48
    delete(m, "Answer")
    fmt.Println("The value:", m["Answer"]) // The value: 0
    v, ok := m["Answer"]
    fmt.Println("The value:", v, "Present?", ok) // The value: 0 Present? false
    

    函数(高级)

    // 可变参数
    func myfunc(args ...int) { // args 的类型是 []int
        for _, arg := range args {
            fmt.Println(arg)
        }
    }
    myfunc(2, 3, 4)
    myfunc(1, 3, 7, 13)
    // 函数类型
    func compute(fn func(float64, float64) float64) float64 {
        return fn(3, 4)
    }
    // 匿名函数
    hypot := func(x, y float64) float64 {
        return math.Sqrt(x*x + y*y)
    }
    fmt.Println(hypot(5, 12)) // 13
    fmt.Println(compute(hypot)) // 5
    fmt.Println(compute(math.Pow)) // 81
    // 闭包
    func adder() func(int) int {
        sum := 0
        return func(x int) int {
            sum += x
            return sum
        }
    }
    pos, neg := adder(), adder()
    for i := 0; i < 10; i++ {
        fmt.Println(pos(i), neg(-2*i))
    }
    /*
    0 0
    1 -2
    3 -6
    6 -12
    10 -20
    15 -30
    21 -42
    28 -56
    36 -72
    45 -90
    */
    

    方法

    // 带接收者(receiver)的方法
    type Vertex struct {
        X, Y float64
    }
    func (v Vertex) Abs() float64 {
        return math.Sqrt(v.X*v.X + v.Y*v.Y)
    }
    v := Vertex{3, 4}
    fmt.Println(v.Abs()) // 5
    // 非结构类型也可以定义方法
    type MyFloat float64
    func (f MyFloat) Abs() float64 {
        if f < 0 {
            return float64(-f)
        }
        return float64(f)
    }
    f := MyFloat(-math.Sqrt2)
    fmt.Println(f.Abs()) // 1.4142135623730951
    // 方法的接收者为指针
    func (v *Vertex) Scale(f float64) {
        v.X = v.X * f
        v.Y = v.Y * f
    }
    v := Vertex{3, 4}
    v.Scale(10)
    fmt.Println(v.Abs()) // 50
    // 接收者为值的方法也可以用指针来调用
    // 接收者为指针的方法也可以用值来调用
    var v Vertex
    v.Scale(5)  // OK
    fmt.Println(v.Abs()) // OK
    p := &v
    p.Scale(10) // OK
    fmt.Println(p.Abs()) // OK
    // 接收者为指针的方法可以修改接收者的值
    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)
    }
    
  • 相关阅读:
    2016-8-29
    2016-8-25
    2016-8-24
    2016-8-23
    2016-8-22
    2016-8-16
    2016-8-15
    深圳_多测师面试 __腾讯云/_高级讲师肖sir
    深圳_多测师面试 _新字节跳动(2020年10月23日)_高级讲师肖sir
    多测师讲解自动化 _RF_(202)高级讲师肖sir
  • 原文地址:https://www.cnblogs.com/zwvista/p/9484645.html
Copyright © 2011-2022 走看看