Golang builtin.append() function example
package builtin
The append built-in function appends elements to the end of a slice. If it has sufficient capacity, the destination is resliced to accommodate the new elements. If it does not, a new underlying array will be allocated. Append returns the updated slice. It is therefore necessary to store the result of append, often in the variable holding the slice itself:
Golang builtin. append() function usage example
package main
import "fmt"
func main() {
slice1 := []string(nil)
fmt.Println("slice1 has elements of ", slice1)
slice1 = append(slice1, "a")
fmt.Println("After append, now slice1 has elements of ", slice1)
}
Output :
slice1 has elements of []
After append, now slice1 has elements of [a]
Reference :
Advertisement
Something interesting
Tutorials
+5.2k Golang : The Tao of importing package
+46.5k Golang : Marshal and unmarshal json.RawMessage struct example
+11.7k How to tell if a binary(executable) file or web application is built with Golang?
+31.1k Golang : Calculate percentage change of two values
+13.6k Golang : Get user input until a command or receive a word to stop
+9.1k Golang : Simple histogram example
+24k Golang : Call function from another package
+8.1k Golang : Tell color name with OpenCV example
+6.2k Golang & Javascript : How to save cropped image to file on server
+5.8k Golang : Markov chains to predict probability of next state example
+28.6k Get file path of temporary file in Go
+8.9k Golang : What is the default port number for connecting to MySQL/MariaDB database ?