How to copy one array into another array in Golang
In Golang , an array is a fixed-length sequence containing elements of a specific type. Unlike slices, arrays have a constant size, which is determined when the array is declared. Copying one array to another is simple but requires both arrays to be of the same length and type.
For example:
package main import "fmt" // Mảng cơ bản được sử dụng trong tất cả ví dụ var source = [5]int{10, 20, 30, 40, 50} func main() { fmt.Println("Source Array:", source) }
Syntax:
for i := 0; i < len(source); i++ { destination[i] = source[i] }
Use loop to copy an array
Go doesn't provide a built-in copy() function for arrays, so the most common way to copy an array is to iterate through each element and assign that element manually.
Syntax
for i := 0; i < len(source); i++ { destination[i] = source[i] }
For example:
package main import "fmt" var source = [5]int{10, 20, 30, 40, 50} func main() { // Tạo mảng đích có cùng kích thước như mảng nguồn var destination [5]int // Tự tay sao chép từng phần tử for i := 0; i < len(source); i++ { destination[i] = source[i] } fmt.Println("Source:", source) fmt.Println("Destination:", destination) }
Result:
Source: [10 20 30 40 50] Destination: [10 20 30 40 50]
Direct Assignment (Works only with Arrays, Does not work with Slices)
You can assign one array to another if they have the same type and length. This method does not work with slices.
Syntax
destination = source
For example
package main import "fmt" var source = [5]int{10, 20, 30, 40, 50} func main() { // Sao chép bằng cách gán trực tiếp var destination [5]int = source fmt.Println("Source:", source) fmt.Println("Destination:", destination) }
Result
Source: [10 20 30 40 50] Destination: [10 20 30 40 50]
Use Pointers (If Array is Large)
If you are working with large arrays and want to avoid copying, you can use a pointer to refer to the source array. This will not create a new array but will point to the memory location of the existing array.
Syntax
destination = &source
For example
package main import "fmt" var source = [5]int{10, 20, 30, 40, 50} func main() { // Tạo một con trỏ tới mảng nguồn var destination *[5]int = &source fmt.Println("Source:", source) fmt.Println("Destination Array via pointer:", *destination) }
Result
Source: [10 20 30 40 50] Destination Array via pointer: [10 20 30 40 50]
Note:
- Direct assignment only works with arrays of the same type and length. If you try to assign arrays of different sizes or types, you will get a compile error.
- Using a pointer does not create a new array; it just references the existing array.
You should read it
- What is Golang? Things you need to know about Golang programming language
- What is Golang? Why should you use Golang language?
- What is the difference between Go and Java?
- How to create basic program in Golang
- How to Install Go on Windows
- How to implement a graph data structure in Golang
- Difference between Go and Python
- Malware that specializes in eavesdropping and sabotage is discovered hiding on Telegram
May be interested
- Introduction to 2D Array - 2-dimensional array in JavaScriptin the following article, we will introduce and show you some basic operations to create and access 2-dimensional arrays - 2d array in javascript. essentially, a 2-dimensional array is a concept of a matrix of matrices - a matrix, used to store information. each 1 element contains 2 separate indexes: row (y) - column and column (x) - column.
- Functions are fields in Golangin golang, you can define functions as fields in a struct. this feature allows you to bind behavior (methods) directly to data types.
- Structural equality in Golangin go language, you are allowed to compare two structures if they are of same type and contain same field values with the help of == operator or deeplyequal() method.
- Arrays in Golangarrays in golang or go programming language are quite similar to other programming languages. here are the things you need to know about arrays in golang.
- Anonymous structures and fields in Golanganonymous structs in golang are temporary structures with no names used for one-time purposes, while anonymous fields allow embedding of unnamed fields.
- Recommended fields in Golangin go structs, fields are encouraged to be the same as anonymous fields, the type of the field is the name of the field.
- Complex String Slice in Golangthere are two terms slice and composite literal. in this article we will learn how to create a slice and use composite literal in golang.
- What is the difference between Go and Java?is golang better than java? is golang harder than java? can golang replace java? this comparison will give you the answer.
- Array in Pythonarrays are a fundamental part of all programming languages, it is a collection of elements of a single data type, for example, integer arrays, string arrays. however, in pythong, there is no original array data structure. so we use python lists instead of arrays.
- How to create basic program in Golanghello, world! is the first basic program in any programming language. you can write this first program in golang by following the steps below.