Skip to main content

Different methods of copying an array using Golang

Example for copying an Array by value: 

package main
import "fmt"
func main() {
	// declare an array
	arr1 := [2]int{1, 2}
	// copying the array by value
	arr2 := arr1
	// change the 1st index value of the original declared array, it won't reflect on copyed array
	arr1[0] = 10
	fmt.Println("Array 1 values :", arr1)
	fmt.Println("Array 2 values :", arr2)
}
Output: 
Array 1 values : [10 2]
Array 2 values : [1 2]

Copying an array by reference:

package main
import "fmt"
func main() {
	// declare an array
	arr1 := [2]int{1, 2}
	// copying the array value by references
	arr2 := &arr1
	// change the 1st index value of the original declared array, it will reflect on copyed array 
	// because the arr2 holds value of arr1 address
	arr1[0] = 10
	fmt.Println("Array 1 values :", arr1)
	fmt.Println("Array 2 values :", *arr2)
}
Output: 
Array 1 values : [10 2]
Array 2 values : [10 2]

Using Copy function:

package main
import "fmt"
func main() {
	arr1 := []int{1, 2}
	arr2 := make([]int, len(arr1))
	copy(arr2, arr1)
	fmt.Println("Array 1 values :", arr1)
	fmt.Println("Array 2 values :", arr2)
}
Output: 
Array 1 values : [1 2]
Array 2 values : [1 2]

Using append function:

package main
import "fmt"
func main() {
	arr1 := []int{1, 2}
	arr2 := make([]int, 0, len(arr1))
	arr2 = append(arr2, arr1...)
	fmt.Println("Array 1 values :", arr1)
	fmt.Println("Array 2 values :", arr2)
}
Output: 
Array 1 values : [1 2]
Array 2 values : [1 2]

Comments

Popular posts from this blog

Why Go Language?

Go Language: Open Source Programming language Statically Typed language Makes sharing code easy Similar to C programming language Organization that use Go includes Google, Docker, Kubernetes, Cloudflare, Dropbox, Netflix & Uber. Why Go language? C like Syntax Compiles to native code i.e. one executable file is need to run the whole program. Garbage collection Concurrency built-in language Compatibility promise → Once a program written to the go one specification will continue to compile and run correctly, over the lifetime of that specification. Why Go Compiler is fast? Simple and Minimalistic (20 keywords only) Does not allows unused dependencies No circular dependencies Does not use header files Solving Modern problems with Go: Go has concise syntax with few keywords to remember. Languages like C or C++ offers fast execution, whereas languages like Ruby or Python offers rapid application development, Go bridges these computing worlds and offers development fast. Modern Computers ...

Copy function in Golang

How Copy function works ? The built-in copy function copies the source element into the destination element and returns the destination element length.   func Copy( dst , src [] Type) int There are some special case using this function which is we can copy a string into an byte slice variable   func Copy( dst []byte, src string) int Examples: package main import "fmt" func main() { var arr = make([]string, 2) n := copy(arr, []string{"hi", "hello", "welcome", "ll"}) fmt.Println("element length :", n) fmt.Println("values :", arr) } Output: element length : 2 values : [hi hello] Special Case example: package main import "fmt" func main() { var b = make([]byte, 5) // copy from a string to byte slice n := copy(b, "vicky") fmt.Println("element length :", n) fmt.Println("values :", string(b)) } Output: element length : 5 values : vicky

Ganache Workspace Overview

Previous Topic:  Ganache Installation Ethereum Workspace Overview: After the workspace created, you will able to see the server details and list of accounts. Each account has 100 ether. There were some additional interface you can access: Accounts       - list of generated accounts and their balance [default view] Blocks       - shows each block mined on the blockchain, along with gas used and transactions. Transactions       - list all the transactions on the blockchain network Contracts      - list the contracts contains in the workspace only when the workspace is connected with a             truffle project. Logs      - shows the logs for the server which is helpful for debugging  Events      - list all the events triggered since the workspace created. How to Connect with Local Ganache Server? In the Ganache UI, you can able to see the server details....