golang \u6570\u7ec4\u9762\u8bd5\u9898

本文深入探讨了Go语言中切片(slice)的底层扩容机制,解释了make函数如何初始化切片,并详细分析了切片长度与容量的关系。通过实际代码示例,展示了切片在达到初始容量限制后的自动扩容过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

\u7b2c\u4e00\u9898 array<\/code> append\u6269\u5bb9\u95ee\u9898<\/h2>\n

\u5b9e\u4f8b\u4ee3\u7801array_append.go<\/code><\/p>\n

\/**\n  * Author: JeffreyBool\n  * Date: 2019\/4\/17\n  * Time: 16:16\n  * Software: GoLand\n*\/\n\npackage array\n\n\/**\n * arr \u5e95\u5c42\u6269\u5bb9\u77e5\u8bc6\u70b9\n *\/\nfunc ArrayAppend() []int {\n    arr := make([]int,5)\n    arr = append(arr,10)\n    \/\/\u95ee\u73b0\u5728 arr \u7ed3\u679c\u662f\u4ec0\u4e48\n    return arr\n}<\/code><\/pre>\n

\u6d4b\u8bd5\u4ee3\u7801array_append_test.go<\/code><\/p>\n

\/**\n  * Author: JeffreyBool\n  * Date: 2019\/4\/17\n  * Time: 16:18\n  * Software: GoLand\n*\/\n\npackage array\n\nimport (\n    \"testing\"\n)\n\nfunc TestArray_append(t *testing.T) {\n    arr := ArrayAppend()\n    t.Log(arr)\n}<\/code><\/pre>\n

\u672c\u9898\u8bb2\u89e3<\/h3>\n

make<\/code> \u51fd\u6570<\/h4>\n

make<\/code> \u4e5f\u662f\u5185\u5efa\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u4ece http:\/\/golang.org\/pkg\/builtin\/#make<\/a> \u770b\u5230\u5b83\uff0c \u5b83\u7684\u51fd\u6570\u539f\u578b \u6bd4 new<\/code> \u591a\u4e86\u4e00\u4e2a\uff08\u957f\u5ea6\uff09\u53c2\u6570\uff0c\u8fd4\u56de\u503c\u4e5f\u4e0d\u540c\u3002
\n\u51fd\u6570\u539f\u578b\u662f<\/p>\n

func make(Type, size IntegerType) Type <\/code><\/pre>\n
\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u7c7b\u578b\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u957f\u5ea6<\/li>\n\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u7c7b\u578b<\/li>\n<\/ul>\n\n

\u5b98\u65b9\u63cf\u8ff0\u4e3a\uff1a<\/p>\n

\n

The make built-in function allocates and initializes an object\uff08\u5206\u914d\u7a7a\u95f4 + \u521d\u59cb\u5316\uff09 of type slice, map or chan(only)<\/strong>. Like new , the first arguement is a type, not a value. Unlike new, make\u2019s 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.
\n\u7ffb\u8bd1\u4e3a\uff1a
\n\u5185\u5efa\u51fd\u6570 make \u5206\u914d\u5e76\u4e14\u521d\u59cb\u5316 \u4e00\u4e2a slice, \u6216\u8005 map \u6216\u8005 chan \u5bf9\u8c61\u3002 \u5e76\u4e14\u53ea\u80fd\u662f\u8fd9\u4e09\u79cd\u5bf9\u8c61\u3002 \u548c new \u4e00\u6837\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f \u7c7b\u578b\uff0c\u4e0d\u662f\u4e00\u4e2a\u503c\u3002 \u4f46\u662fmake \u7684\u8fd4\u56de\u503c\u5c31\u662f\u8fd9\u4e2a\u7c7b\u578b\uff08\u5373\u4f7f\u4e00\u4e2a\u5f15\u7528\u7c7b\u578b\uff09\uff0c\u800c\u4e0d\u662f\u6307\u9488\u3002 \u5177\u4f53\u7684\u8fd4\u56de\u503c\uff0c\u4f9d\u8d56\u5177\u4f53\u4f20\u5165\u7684\u7c7b\u578b\u3002<\/p>\n

Slice : \u7b2c\u4e8c\u4e2a\u53c2\u6570 size \u6307\u5b9a\u4e86\u5b83\u7684\u957f\u5ea6\uff0c\u6b64\u65f6\u5b83\u7684\u5bb9\u91cf\u548c\u957f\u5ea6\u76f8\u540c\u3002
\n\u4f60\u53ef\u4ee5\u4f20\u5165\u7b2c\u4e09\u4e2a\u53c2\u6570 \u6765\u6307\u5b9a\u4e0d\u540c\u7684\u5bb9\u91cf\u503c\uff0c\u4f46\u662f\u5fc5\u987b\u4e0d\u80fd\u6bd4\u957f\u5ea6\u503c\u5c0f\u3002
\n\u6bd4\u5982: make([]int, 0, 10)
\nMap: \u6839\u636esize \u5927\u5c0f\u6765\u521d\u59cb\u5316\u5206\u914d\u5185\u5b58\uff0c\u4e0d\u8fc7\u5206\u914d\u540e\u7684 map \u957f\u5ea6\u4e3a0\u3002 \u5982\u679c size \u88ab\u5ffd\u7565\u4e86\uff0c\u90a3\u4e48\u4f1a\u5728\u521d\u59cb\u5316\u5206\u914d\u5185\u5b58\u7684\u65f6\u5019 \u5206\u914d\u4e00\u4e2a\u5c0f\u5c3a\u5bf8\u7684\u5185\u5b58\u3002
\nChannel: \u7ba1\u9053\u7f13\u51b2\u533a\u4f9d\u636e\u7f13\u51b2\u533a\u5bb9\u91cf\u88ab\u521d\u59cb\u5316\u3002\u5982\u679c\u5bb9\u91cf\u4e3a 0 \u6216\u8005\u88ab \u5ffd\u7565\uff0c\u7ba1\u9053\u662f\u6ca1\u6709\u7f13\u51b2\u533a\u7684\u3002<\/p>\n<\/blockquote>\n

\u6839\u636e\u4e0a\u9762 make \u7684\u89e3\u91ca\u6211\u4eec\u53ef\u4ee5\u5f97\u77e5Slice\u53ef\u4ee5\u8bbe\u5b9a\u4e09\u4e2a\u53c2\u6570, \u7b2c\u4e00\u4e2a\u662f\u7c7b\u578b,\u7b2c\u4e8c\u4e2a\u662f\u957f\u5ea6,\u7b2c\u4e09\u4e2a\u662f\u5927\u5c0f, \u5f53Slice \u5927\u5c0f\u4e0d\u591f\u4e86\u4f1a\u81ea\u52a8\u6269\u5bb9.<\/li>\n<\/ul>\n\n
arr := make([]int,5) \n\/\/\u5b9e\u9645\u8bd1\u4e3a\narr := make([]int,5,5)<\/code><\/pre>\n

arr \u7684\u5927\u5c0f,\u957f\u5ea6\u90fd\u4e3a5.<\/p>\n

\u6211\u4eec\u5c06\u4ee3\u7801\u7a0d\u5fae\u6539\u826f\u4e0b:<\/p>\n

\/**\n * Author: JeffreyBool\n * Date: 2019\/4\/17\n * Time: 16:16\n * Software: GoLand\n*\/\n\npackage array\n\nimport (\n \"fmt\"\n)\n\n\/**\n * arr \u5e95\u5c42\u6269\u5bb9\u77e5\u8bc6\u70b9\n *\/\nfunc ArrayAppend() []int {\n arr := make([]int,5)\n fmt.Printf(\"arr.len: %d; arr.cap: %d \\n\
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值