[Go]Slice

While array in Go is fixed size, slice on the other hand is not. You declare a variable without specifying the size, and if new element is needed use the append() function to add element to each index.

package main

import "fmt"

func main() {
	// slice does not require the need to declare the size
	// this is more flexible.
	var slices [] int
	for i:=0; i<10; i++ {
		// new element is added using the append() function.
		slices = append(slices, i)
		fmt.Printf("%d\n", slices[i])
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s