Go pkg學與練 - buildin
來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。builtin package 包含go語言中的各種類型:<pre><code>unint8, uint16, uint32, uint64int8, int16, int32, int64float32, float64, complex64, complex128string, int, uintptr, byte, runeconst iota = 0, niltype error interface {}</code></pre>以及常用的內建函數:```func append(slice []Type, elems ...Type) []Typefunc copy(dst, src []Type) intfunc delete(m map[Type]Type1, key Type)// array, pointer, slice, string, channelfunc len(v Type) intfunc cap(v Type) int// slice, map, chanfunc make(t Type, size ...IntegerType) Typefunc new(Type) *Typefunc complex(r, i FloatType) ComplexTypefunc real(c ComplexType) FloatTypefunc imag(c ComplexType) FloatTypefunc close(c chan<- Type)func panic(v interface{})func recover() interface{}func print(args ...Type)func println(args ...Type)```以下是一個簡單的使用內建函數的程式:~~~package mainfunc main() {// make a slice, copy, lens := 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 channelc := 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")}}~~~217 次點擊 ∙ 1 贊