How to get the first element of a slices in golang?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

To get the first element of a slices, use slice[0] it will return first element of slice.

Here, we are using access slice element by it’s index concept.

In Slices, first element stores in the 0th index.

Let’s start

package main
import "fmt"

func main() {
  slice := []int{1,2,3,4,5,6}
	fmt.Println("First Element", slice[0])
}

In the above program, we are trying to access value from their key, let’s see the output.

Output:

First Element 1

Try it yourself

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

I'm working on some more Cheat Sheets book on Jquery, TypeScript, React js and for other languages. I will send it once it's completed.

Related Posts