25% off

Use code FUNCMAIN at checkout for 25% off all premium courses.

Get started →

Arrays and Slices in Go

May 8, 2021
Elliot Forbes

Elliot Forbes

Course Instructor

Hey Gophers! My name is Elliot and I'm the creator of TutorialEdge and I've been working with Go systems for roughly 5 years now.

Code

package main

import "fmt"

func main() {
	println("Arrays and Slices in Go")

	// [1, 2, 3, 4]
	planets := [8]string{"mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "nepture"}
	fmt.Println(planets)

	var planetsArray [8]string
	planetsArray[0] = "mercury"
	fmt.Println(planetsArray)

	planetSlice := []string{"mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "nepture"}
	fmt.Println(planetSlice)

	var planetSliceVerbose []string
	planetSliceVerbose = append(planetSliceVerbose, "mercury")
	fmt.Println(planetSliceVerbose)
}

Output

$ go run main.go
Arrays and Slices in Go
[mercury venus earth mars jupiter saturn uranus nepture]
[mercury       ]
[mercury venus earth mars jupiter saturn uranus nepture]
[mercury]