or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Go Go Power Slice! - Yu-Lang Chu
tags:
COSCUP2020
入門
TR214
歡迎來到 https://hackmd.io/@coscup/2020 共筆
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →點擊本頁上方的 開始用 Markdown 一起寫筆記!
手機版請點選上方 按鈕展開議程列表。
Array and slice
The length is a part of the array's type
Slice 的定義:敘述底層的 array
要操作的是底層的 Array ,為何還需要 slice ?
Slice 包含一個指向 array 的指標與一個長度、容量
Slice 的長度跟底層 Array 沒有直接掛勾
array 賦值是 copy 而不是 reference
Append behind
append 到 slice ,底層的 array 可能產生變動
slice 是 pass by value 還是 call by value
slice 傳入 function 實會 copy 新的 slice,但是會 copy 指針(指向同樣的 array)
slice 可能是指向 array ,或是 array 的一部分
Compare
deep comapre 效能不太好
difference with other languages
Go slice 可自由控制長度與容量
nil slice is just a zero length/cap slice and what the difference with empty slice
nil
slice 作長度和容量檢查,可以當成一般 slice 操作append
擴容流程:
String 可以直接附加到 byte slice
擴容策略:
Beavior of slice
Slice 長度 == 容量時作 append ,會進行擴容,同時創造新的 array
Common mistake
Memory leak
單純改變 Slice 長度卻沒有清空
Append overwrites value(state slice )
mistake:
Performance tips
allocate enough memory
assign 足夠容量:使用 append 時不會產生新 array
Make-Copy vs append
Reuse allocated memory :清空的方法
implemenetation of queue
成長到一定長度就需要 new 新的 array
String & Byte
String 本質上是 Byte Slice
合法操作:
額外發現:直接用 String 比 Byte Slice 快(因為轉型需要花時間)
結論:不要一直轉形,因為會一直建立和毀滅 array