23

I have initialized a struct :

type DayAndTime struct {
    days string
    time string
  }

I have initialized an empty set of array of this DayAndTime type:

day := []DayAndTime{}

And put a value in it: day[0] = DayAndTime{"Monday", "8.00 PM"}

But it shows a runtime error:

panic: runtime error: invalid memory address or nil pointer dereference

Why is this happenning and what could be a possible solution?

edit: It's actually a slice not an array.

2

4 Answers 4

36

Here you have a zero-length slice, the len and cap functions will both return 0 for a zero-valued slice

A slice cannot be grown beyond its capacity. Attempting to do so will cause a runtime panic, just as when indexing outside the bounds of a slice or array. Similarly, slices cannot be re-sliced below zero to access earlier elements in the array.

You may us make to initialize the slice with capacity and assign with index or use append to add values to slice

Both are valid code

var day []DayAndTime
day = append(day, DayAndTime{"Monday", "8.00 PM"})

or

var day = make([]DayAndTime, 1)
day[0] = DayAndTime{"Monday", "8.00 PM"}

Using append is recomended

Here is a sample code justifying/explaining the answer https://play.golang.org/p/ajsli-6Vqw

package main

import (
    "fmt"
)

type DayAndTime struct {
    days string
    time string
}

func ZeroLength() {
    var day = []DayAndTime{}
    fmt.Println("Hello, playground", cap(day), len(day), day)
}

func AppendArray() {
    var day = []DayAndTime{}
    day = append(day, DayAndTime{"Monday", "8.00 PM"})
    fmt.Println("Hello, playground", cap(day), len(day), day)
}

func SetIndex() {
    var day = make([]DayAndTime, 1)
    day[0] = DayAndTime{"Monday", "8.00 PM"}
    fmt.Println("Hello, playground", cap(day), len(day), day)
}

func main() {
    ZeroLength()
    AppendArray()
    SetIndex()
}
2
  • 2
    day := []DayAndTime{} --- it's not nil here though.
    – zerkms
    Commented Sep 17, 2017 at 7:23
  • @zerkms yes it is a zero length slice Commented Jul 24, 2020 at 10:56
0

If you print the length of day, you will get 0, so you cannot access day[0], which does not exist.

You can write it in this way: day = append(day, DayAndTime{"Monday", "8.00 PM"})

0

This should be the right syntax in your example:

type DayAndTime struct {
    days string
    time string
}

days := []*DayAndTime{}
-1

You asked for a solution for arrays, but in your code you show a slice.

foo := []string{}
t := reflect.TypeOf(foo)
fmt.Println(t.Kind()) // slice

bar := [2]string{}
tt := reflect.TypeOf(bar)
fmt.Println(tt.Kind()) // array

Anyway, ... you got that error because days[0] is out of range in the slice. Thant actually contains zero item and its length is zero. You have to use append, that return new slice, and then you can access to days[0]. Or can initialize with make.

package main

import "fmt"

type DayAndTime struct {
    days string
    time string
}

func main() {
    days := []DayAndTime{}
    days = append(days, DayAndTime{"days ...", "time ..."})
    fmt.Printf("%v", days[0].days)
}

Another solution is to build an array instead of slice

func main() {
    days := [2]DayAndTime{}
    days[0].days = "fooo"
    fmt.Printf("%v", days[0].days)
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.