Golang 拷貝Array或Slice的操作
Golang中Array是值類型而slice是引用類型。因此兩者之間的賦值或拷貝有些差異,本文帶你了解各自的差異。
1. 拷貝array
前面提及數(shù)組是值類型,所以數(shù)組變量名不是指向第一個元素的指針。事實上它表示整個數(shù)組,下面兩者情況將自動創(chuàng)建數(shù)組:
數(shù)組變量賦值給另一個數(shù)組變量
數(shù)組變量傳遞作為函數(shù)參數(shù)
請看示例:
package main import "fmt" func main() { sample1 := [2]string{"a", "b"} fmt.Printf("Sample1 Before: %v\n", sample1) sample2 := sample1 sample2[0] = "c" fmt.Printf("Sample1 After assignment: %v\n", sample1) fmt.Printf("Sample2: %v\n", sample2) test(sample1) fmt.Printf("Sample1 After Test Function Call: %v\n", sample1) } func test(sample [2]string) { sample[0] = "d" fmt.Printf("Sample in Test function: %v\n", sample) }
輸出結(jié)果:
Sample1 Before: [a b]
Sample1 After assignment: [a b]
Sample2:
Sample in Test function: [d b]
Sample1 After Test Function Call: [a b]
我們稍作解釋:
sample1 賦給 sample2 ,然后修改sample2中第一個元素。打印sample1驗證是否有影響,當(dāng)然沒有改變。這是因為sample1 賦給 sample2,會創(chuàng)建sample1的拷貝給sample2,故修改sample2不影響sample1.
傳遞sample1給test函數(shù),然后在函數(shù)體內(nèi)修改其第一個元素。之后打印sample1驗證是否有影響,當(dāng)然也沒有。原因是一樣的,當(dāng)sample1作為參數(shù)傳遞給test時,sample1的拷貝被創(chuàng)建并傳入,因此修改不會影響原來sample1.
2. 拷貝slice
Golang內(nèi)置包提供copy函數(shù)能夠拷貝slice,函數(shù)前面如下,其返回拷貝元素個數(shù):
func copy(dst, src []Type) int
使用copy函數(shù)需要考慮兩種情況:
如果src長度大于dst,那么僅拷貝dst長度個元素
如果dst長度大于src,那么僅拷貝src長度個元素
總結(jié)為拷貝兩者最小長度元素:min(src,dst)
需要注意的是,一旦拷貝完成,對目標(biāo)的修改不會影響源,反之亦然。
我們也通過示例說明:
package main import "fmt" func main() { src := []int{1, 2, 3, 4, 5} dst := make([]int, 5) numberOfElementsCopied := copy(dst, src) fmt.Printf("Number Of Elements Copied: %d\n", numberOfElementsCopied) fmt.Printf("dst: %v\n", dst) fmt.Printf("src: %v\n", src) //After changing numbers2 dst[0] = 10 fmt.Println("\nAfter changing dst") fmt.Printf("dst: %v\n", dst) fmt.Printf("src: %v\n", src) }
輸出如下:
Number Of Elements Copied: 5
dst: [1 2 3 4 5]
src: [1 2 3 4 5]
After changing dst
dst: [10 2 3 4 5]
src: [1 2 3 4 5]
3. 總結(jié)
本文介紹了Go Array和Slice直接拷貝。Array是值類型直接賦值即拷貝,Slice是引用類型,直接賦值是指針會影響源Slice,但可以通過內(nèi)置copy函數(shù)實現(xiàn)賦值功能。
補充:golang字節(jié)數(shù)組拷貝BlockCopy函數(shù)實現(xiàn)
在C#中,Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) 函數(shù)使用比較廣泛,其含義:
將指定數(shù)目的字節(jié)從起始于特定偏移量的源數(shù)組復(fù)制到起始于特定偏移量的目標(biāo)數(shù)組。
參數(shù) src Array 源緩沖區(qū)。 srcOffset Int32 src 中的字節(jié)偏移量,從零開始。 dst Array 目標(biāo)緩沖區(qū)。 dstOffset Int32 dst 中的字節(jié)偏移量,從零開始。 count Int32 要復(fù)制的字節(jié)數(shù)。
go語言中實現(xiàn)如下:
func blockCopy(src []byte, srcOffset int, dst []byte, dstOffset, count int) (bool, error) { srcLen := len(src) if srcOffset > srcLen || count > srcLen || srcOffset+count > srcLen { return false, errors.New("源緩沖區(qū) 索引超出范圍") } dstLen := len(dst) if dstOffset > dstLen || count > dstLen || dstOffset+count > dstLen { return false, errors.New("目標(biāo)緩沖區(qū) 索引超出范圍") } index := 0 for i := srcOffset; i < srcOffset+count; i++ { dst[dstOffset+index] = src[srcOffset+index] index++ } return true, nil }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
- go語言中切片Slice與數(shù)組Array對比以及panic:?runtime?error:?index?out?of?range問題解決
- Golang?range?slice?與range?array?之間的區(qū)別
- GO中?分組聲明與array,?slice,?map函數(shù)
- Go 修改map slice array元素值操作
- Golang學(xué)習(xí)筆記(四):array、slice、map
- Go語言入門教程之Arrays、Slices、Maps、Range操作簡明總結(jié)
- 理解Golang中的數(shù)組(array)、切片(slice)和map
- Go語言中的Array、Slice、Map和Set使用詳解
- GO語言的數(shù)組array與切片slice詳解
相關(guān)文章
Goland遠程連接Linux進行項目開發(fā)的實現(xiàn)
有的時候我們的開發(fā)代碼要在linux服務(wù)器上運行,本文主要介紹了Goland遠程連接Linux進行項目開發(fā)的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-06-06