Go pkg学与练 - buildin

Stanley · 2018-03-29 14:11:56 · 1002 次点击 · 预计阅读时间 2 分钟 · 大约8小时之前 开始浏览    
这是一个创建于 2018-03-29 14:11:56 的文章,其中的信息可能已经有所发展或是发生改变。

builtin package 包含go语言中的各种类型:

unint8, uint16, uint32, uint64
int8, int16, int32, int64
float32, float64, complex64, complex128
string, int, uintptr, byte, rune
const iota = 0, nil
type error interface {}

以及常用的内置函数:

func append(slice []Type, elems ...Type) []Type
func copy(dst, src []Type) int
func delete(m map[Type]Type1, key Type)
// array, pointer, slice, string, channel
func len(v Type) int
func cap(v Type) int
// slice, map, chan
func make(t Type, size ...IntegerType) Type
func new(Type) *Type
func complex(r, i FloatType) ComplexType
func real(c ComplexType) FloatType
func imag(c ComplexType) FloatType
func close(c chan<- Type)
func panic(v interface{})
func recover() interface{}
func print(args ...Type)
func println(args ...Type)

以下是一个简单的使用内置函数的程序:

package main

func main() {
    // make a slice, copy, len
    s := make([]string, 0)
    s = append(s, "Apple")
    s = append(s, "Orange", "Peach")

    println("len of s: ", len(s))
    for i, v := range s {
        println(i, v)
    }

    s2 := make([]string, 5)
    n := copy(s2, s)

    println("len of s2: ", len(s2))
    println("copied len: ", n)
    for i, v := range s2 {
        println(i, v)
    }

    // make a channel, close a channel
    c := make(chan string, 2)
    c <- "Dolphin"
    c <- "Shark"

    println(<-c)
    println(<-c)

    c <- "Seal"
    println(<-c)
    close(c)

    if _, ok := <-c; ok {
        println("Channel is open")
    } else {
        println("Channel is closed")
    }
}

有疑问加站长微信联系(非本文作者))

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1002 次点击  ∙  1 赞  
加入收藏 微博
上一篇:golang闭包
下一篇:golang不定参数
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传