• go内建方法 append copy delete


    package main

    import "fmt"

    func main() {

    testAppend()
    testCopy()
    testDelete()
    }

    func testAppend() {

    var sl = make([]int,2)
    sl[0] = 1
    sl[1] = 2
    fmt.Println(len(sl))
    fmt.Println(cap(sl))
    sl = append(sl, 3)
    //sl = append(sl, 4)
    fmt.Println(sl)
    fmt.Println(len(sl))
    fmt.Println(cap(sl))
    }

    func testCopy() {
    var sl1 = make([]string,3)
    var sl2 = make([]string,2)

    sl1[0] = "a"
    sl1[1] = "b"
    sl1[2] = "e"
    sl2[0] = "c"
    sl2[1] = "d"

    copy(sl1,sl2)
    fmt.Println(sl1)

    }

    func testDelete() {
    var sl = make(map[int]int ,2)
    sl[1] = 1
    sl[2] = 2
    sl[3] = 3

    delete(sl, 2)
    fmt.Println(sl)
    }

    结果如下

    GOROOT=/usr/local/go #gosetup
    GOPATH=/www/gopath #gosetup
    /usr/local/go/bin/go build -i -o /private/var/folders/fc/4txmmczj6q92p6058h3w7t_80000gn/T/___go_build_main_go__2_ /www/go/learn/main.go #gosetup
    /private/var/folders/fc/4txmmczj6q92p6058h3w7t_80000gn/T/___go_build_main_go__2_ #gosetup
    2
    2
    [1 2 3]
    3
    4
    [c d e]
    map[1:1 3:3]

    Process finished with exit code 0

  • 相关阅读:
    js倒计时的实现
    用Math获取随机数的方法抽奖
    计算器的实现
    放大镜
    关于轮播图,我知道的不多。
    jqery标签页
    jQuery鼠标划入划出
    说说手机页面
    简单说说tab标签页和轮播图
    前端中的那些小事
  • 原文地址:https://www.cnblogs.com/brady-wang/p/13056227.html
Copyright © 2020-2023  润新知