33

My struct

type Result struct {
   name   string
   Objects []struct {
       id int
   }
}

Initialize values for this

func main() {
   var r Result;
   r.name  = "Vanaraj";
   r.Objects[0].id = 10;
   fmt.Println(r)
}

I got this error. "panic: runtime error: index out of range"

How to fix this?

4 Answers 4

32

Firstly, I'd say it's more idiomatic to define a type for your struct, regardless of how simple the struct is. For example:

type MyStruct struct {
    MyField int
}

This would mean changing your Result struct to be as follows:

type Result struct {
    name   string
    Objects []MyStruct
}

The reason your program panics is because you're trying to access an area in memory (an item in your Objects array) that hasn't been allocated yet.

For arrays of structs, this needs to be done with make.

r.Objects = make([]MyStruct, 0)

Then, in order to add to your array safely, you're better off instantiating an individual MyStruct, i.e.

ms := MyStruct{
    MyField: 10,
}

And then appending this to your r.Objects array

r.Objects = append(r.Objects, ms)

For more information about make, see the docs

Sign up to request clarification or add additional context in comments.

3 Comments

length must be specified in make, isn't it? make([]MyStruct, 0)
Is there a way without refactoring the code to initialize the array of Objects
@Katiyman if you mean that you want to initialise a list of, say, 10 Objects, I think you could use r.Objects = make([]MyStruct, 10).
28

After defining the struct as suggested in another answer:

type MyStruct struct {
    MyField int
}


type Result struct {
    Name   string
    Objects []MyStruct
}

Then you can initialize a Result object like this:

result := Result{
    Name: "I am Groot",
    Objects: []MyStruct{
        {
            MyField: 1,
        },
        {
            MyField: 2,
        },
        {
            MyField: 3,
        },
    },
}

Full code:

package main

import "fmt"

func main() {
    result := Result{
        Name: "I am Groot",
        Objects: []MyStruct{
            {
                MyField: 1,
            },
            {
                MyField: 2,
            },
            {
                MyField: 3,
            },
        },
    }

    fmt.Println(result)

}

type MyStruct struct {
    MyField int
}

type Result struct {
    Name    string
    Objects []MyStruct
}

You can verify this in this Go playground.

1 Comment

I wonder if it is possible to initialize the array Objects in using make: Objects: make([]MyStruct, 0, 10) in order to say initialize it to a slice?
6

Objects contains no elements. You need to append element first. Like this:

r.Objects = append(r.Objects, struct{ id int }{}) 

Also you can omit r.Objects[0].id = 10; using initialization of your struct like this:

r.Objects = append(r.Objects, struct{ id int }{ 10 })

6 Comments

Thanks Ronindev. type Result struct { name string Objects []struct { id int name string email string } } How to add values for this?
RobinDev, How to add multiple variable in append?
@vanarajcs something like this r.Objects = append(r.Objects, struct{ id int }{10}, struct{ id int }{20})
It's better to use named type for your struct{ id int }, as @matt-schofield suggested
r.Objects = append(r.Objects, struct{ id int, string name}{ 10 "Vanaraj"}) is it right?
|
0

This is probably a dumb idea, but you can roundtrip it through JSON:

package main

import (
   "bytes"
   "encoding/json"
)

type (
   a []interface{}
   m map[string]interface{}
)

func decode(in, out interface{}) {
   var b bytes.Buffer
   json.NewEncoder(&b).Encode(in)
   json.NewDecoder(&b).Decode(out)
}

Example:

package main
import "fmt"

type result struct {
   Name string
   Objects []struct {
      Id int
   }
}

func main() {
   r := m{
      "Name": "Vanaraj",
      "Objects": a{
         m{"Id": 10},
      },
   }
   var s result
   decode(r, &s)
   fmt.Printf("%+v\n", s) // {Name:Vanaraj Objects:[{Id:10}]}
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.