* make和new的區別:
* make只能用來分配及初始化類型為slice,map,chan的數據;new可以分配任意類型的數據
* new分配返回的是指針,即類型\*T;make返回引用,即T
* new分配的空間被清零,make分配后,會進行初始化
* [http://docscn.studygolang.com/doc/effective\_go.html#make分配](http://docscn.studygolang.com/doc/effective_go.html#make%E5%88%86%E9%85%8D)
* [http://docscn.studygolang.com/doc/effective\_go.html#new](http://docscn.studygolang.com/doc/effective_go.html#new)分配
> ### make
~~~
// The make built-in function allocates and initializes an object of type
// slice, map, or chan (only). Like new, the first argument is a type, not a
// value. Unlike new, make's return type is the same as the type of its
// argument, not a pointer to it. The specification of the result depends on
// the type:
// Slice: The size specifies the length. The capacity of the slice is
// equal to its length. A second integer argument may be provided to
// specify a different capacity; it must be no smaller than the
// length, so make([]int, 0, 10) allocates a slice of length 0 and
// capacity 10.
// Map: An empty map is allocated with enough space to hold the
// specified number of elements. The size may be omitted, in which case
// a small starting size is allocated.
// Channel: The channel's buffer is initialized with the specified
// buffer capacity. If zero, or the size is omitted, the channel is
// unbuffered.
func make(Type, size IntegerType) Type
~~~
* 內建函數 make 分配和初始化 一個 slice 或 map 或 chan 對象, 并且只能是這三種對象
* 和 new 類似,第一個參數也是一個類型而不是一個值, 不同的是 make 返回類型的引用而不是指針,而返回值也依賴于具體傳入的類型
* slice : 第二個參數指定它的長度, 此時它的容量和長度相同. 可以用第三個參數來指定不同容量大小,但不能小于它的長度(第二個參數)
* map : 根據size 大小來初始化分配內存,不過分配后的 map 長度為0。 如果 size 被忽略了,那么會在初始化分配內存的時候 分配一個小尺寸的內存
* channel : 管道緩沖區依據緩沖區容量被初始化。如果容量為 0 或者被忽略,管道是沒有緩沖區的。
> ### len
~~~
// The len built-in function returns the length of v, according to its type:
// Array: the number of elements in v.
// Pointer to array: the number of elements in *v (even if v is nil).
// Slice, or map: the number of elements in v; if v is nil, len(v) is zero.
// String: the number of bytes in v.
// Channel: the number of elements queued (unread) in the channel buffer;
// if v is nil, len(v) is zero.
func len(v Type) int
~~~
> ### cap
~~~
// The cap built-in function returns the capacity of v, according to its type:
// Array: the number of elements in v (same as len(v)).
// Pointer to array: the number of elements in *v (same as len(v)).
// Slice: the maximum length the slice can reach when resliced;
// if v is nil, cap(v) is zero.
// Channel: the channel buffer capacity, in units of elements;
// if v is nil, cap(v) is zero.
func cap(v Type) int
~~~
* map變量被創建后,你可以指定map的容量,但是不可以在map上使用cap()方法
> ### new
~~~
// The new built-in function allocates memory. The first argument is a type,
// not a value, and the value returned is a pointer to a newly
// allocated zero value of that type.
func new(Type) *Type
~~~
* 內建函數 new 用來分配內存,它的第一個參數是一個類型,不是一個值,它的返回值是一個指向新分配類型零值的指針
~~~
package main
import "fmt"
func main() {
number1 := [5]int{}
number2 := new([5]int)
fmt.Println(number1)
fmt.Println(number2)
}
[0 0 0 0 0]
&[0 0 0 0 0]
package main
import "fmt"
type person struct {
name string
age int
}
func main() {
p1 := person{}
p2 := &person{}
p3 := new(person)
fmt.Println(p1) // 返回類型
fmt.Println(p2) // 返回指針
fmt.Println(p3) // 和p2一樣
}
{ 0}
&{ 0}
&{ 0}
~~~
> ### nil
~~~
// nil is a predeclared identifier representing the zero value for a
// pointer, channel, func, interface, map, or slice type.
var nil Type // Type must be a pointer, channel, func, interface, map, or slice type
package main
import "fmt"
func main() {
// nil 是一個預定義標識符,其代表(用作)一些類型的零值;這些類型包括:pointer, channel, func, interface, map, slice
var n1 []int
var n2 map[int]string
var n3 chan int
if n1 == nil {
fmt.Println("n1")
}
if n2 == nil {
fmt.Println("n1")
}
if n3 == nil {
fmt.Println("n1")
}
}
~~~
> ### 相關閱讀
* [理解Go語言的nil](http://www.jianshu.com/p/dd80f6be7969)
* [golang: 詳解interface和nil](https://my.oschina.net/goal/blog/194233)
- 第一序 入門教程(一)
- 1.1環境配置
- 1.1 環境配置(補充:Linux下安裝)
- 1.1 環境配置(補充:線上部署)
- 1.2 開發工具GoLand
- 1.3 準備工作
- 1.4 第一個應用程序 Hello World
- 1.4 補充 go get github 超時
- 第二序 入門教程(二)
- 2.1 語法結構
- 2.2 常量, 變量
- 2.2.1 命名規則
- 2.2.2 變量
- 2.2.2 變量(補充:類型推斷的好處)
- 2.2.2 變量(補充:泛型)
- 2.2.3 常量
- 2.2.4 iota
- 2.2.5 Unicode字符編碼
- 2.2.6 GBK 轉 UTF8
- 2.3 條件語句
- 2.3.1 判斷語句 if
- 2.3.2 選擇語句 switch
- 2.3.3 循環語句 for
- 2.3.4 遍歷 range
- 2.3.5 跳轉語句 goto, break, continue
- 2.3.6 for 和 for range區別
- 2.4 數組, 切片, 集合, 通道
- 2.4.1 make, len, cap, new, nil
- 2.4.1 make, len, cap, new, nil (補充:nil)
- 2.4.2 數組 array
- 2.4.3.1 切片 slice - 1
- 2.4.3.2 切片 slice - 2
- 2.4.3.3 slice list ring
- 2.4.4 集合 map
- 2.4.5 goroutine
- 2.4.6 channel
- 2.5 函數, 結構, 方法, 接口
- 2.5.1 函數 function
- 2.5.2 結構 struct
- 2.5.3 方法 method
- 2.5.4 接口 interface
- 2.5.5 Go是面向對象的語言嗎?
- 2.5.6 json序列化和反序列化
- 2.5.7 T和指針T
- 2.6 defer, panic, recover
- 2.6.1 defer
- 2.6.2 painc, recover
- 2.7 指針
- 2.7 指針(補充: 可尋址和不可尋址)
- 2.8 反射
- 第三序 相關閱讀
- 3.1 相關閱讀1
- 3.2 相關閱讀2
- 3.3 相關閱讀3
- 第四序 性能分析和調試工具
- 4.1 pprof工具介紹
- 4.2 CPU信息采集
- 4.3 Heap信息采集
- 4.4 Http信息采集
- 4.5 單元測試(功能測試)
- 4.6 基準測試(壓力測試/性能測試)
- 4.7 示例測試(example)
- 4.8 gdb調試
- 第五序 網絡編程
- 5.1 http請求和響應
- 5.2 socket
- 5.2.1 概念
- 5.2.2 服務端
- 5.2.3 客戶端
- 5.3 WebSocket
- 5.3.1 第一版
- 5.3.1.1 服務端
- 5.3.1.2 客戶端
- 5.3.1.3 相關閱讀
- 5.3.2 服務端
- 5.3.3 客戶端
- 5.3.4 nginx配置
- 5.3.5 修改版
- 5.3.5.1 草稿 - 1
- 5.3.5.2 草稿 - 2
- 5.3.5.3 草稿 - 3
- 5.3.5.4 服務端
- 5.3.5.5 客戶端
- 5.4 打印客戶端頭部信息
- 第六序 算法
- 6.1 查找
- 6.1.1 二分查找
- 6.2 排序
- 6.2.1 交換排序 - 冒泡排序
- 6.2.2 插入排序 - 直接插入排序
- 6.2.3 插入排序 - 希爾排序
- 6.2.4 交換排序 - 快速排序
- 6.3 算法求解應用
- 第七序 微服務
- 7.1 相關閱讀
- 7.2 gRPC
- 7.2.1 準備工作
- 7.2.2 編譯.proto文件
- 7.2.3 gRPC服務端
- 7.2.4 gRPC客戶端
- 7.3 micro/micro
- 7.3.1 服務發現
- 7.3.2 安裝consul
- 7.3.3 準備工作
- 7.3.4 服務端
- 7.3.5 客戶端
- 7.3.6 默認的服務發現
- 7.3.7 文檔閱讀
- 7.4 protobuf序列化
- 第八序 Web
- 8.1 視圖模板
- 8.1.1 main.go
- 8.1.2 login.html
- 8.2 原生留言板
- 8.2.1 原生sql
- 8.2.1.1 main.go
- 8.2.1.2 view
- 8.2.1.2.1 index.html
- 8.2.1.2.2 create.html
- 8.2.2 sqlx
- 8.3 Gin框架
- 第九序 數據庫
- 9.0 資料收集
- 9.1 Redis數據庫 (gomodule/redigo)
- 9.1.1 介紹
- 9.1.2 消息隊列
- 9.2 Redis數據庫(go-redis/redis)
- 第十序 日記
- 10.1 SimplePanic
- 10.2 第一版日記庫
- 10.2.1 winnielog
- 10.2.2 使用
- 第十一序 中間鍵
- 11.0 資料收集
- 11.1 NSQ
- 11.2 zookeeper
- 11.3 kafka
- 第十二序 加密
- 12.1 Token
- 12.2 SHA1
- 2.3 RSA + AES
- 第十三序 分布式鎖
- 第十四序 標準庫練習
- container/list
- 鏈表
- container/ring
- 環形鏈表
- context
- flag (獲取命令行參數)
- io
- strconv
- sync
- 為什么需要鎖?
- 互斥鎖
- 讀寫鎖
- 條件變量
- 計數器
- 并發安全字典
- 自制并發安全字典
- 官方并發安全字典
- 連接池
- sync/atomic
- 原子操作
- 第十五序 其它內容
- 文件讀寫
- 工作池
- 第十六序 相關閱讀