builtin package 包含go语言中的各种类型:
<pre><code>unint8, uint16, uint32, uint64
int8, int16, int32, int64
float32, float64, complex64, complex128
string, int, uintptr, byte, rune
const iota = 0, nil
type error interface {}
</code></pre>
以及常用的内置函数:
```
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")
}
}
~~~
有疑问加站长微信联系(非本文作者))