0

I would like to set a default value and if this is not set in a structure, I would like to set it. It's a bit confusing, but please take a look to the (simplified) code:

package main

import "log"

type Something struct {
    A string
    B map[string]Type
    C Epyt
}
type Type struct {
    A Epyt
    B string
}

type Epyt struct {
    A string
    B string
}

func main() {
    var a Something
    a.A = "Test A (Something)"
//  var a.B["one"] Type
    a.B["one"].A.A = a.B["one"].A.A
    a.B["one"].A.A = "Test A ([one]Type.Epyt)"
    a.B["two"].A.A = "Test A ([two]Type.Epyt)"  
    a.C.A = "Test A (Epyt)"
    a.C.B = "Test B (Epyt)"

    for i := range a.B {
        if a.B[i].A.B == "" {
            a.B[i].A.B = a.C.B
        }
    }
    log.Printf("%+v", a)
}

I'm working with viper to unmarshal a config file, therefor I'm not able to use pointers (or am I wrong?).

The error I'm getting, is cannot assign to struct field *** in map.

I found that this is an old bug in go lang still not corrected.

I've read Why do I get a "cannot assign" error when setting value to a struct as a value in a map? and Golang: I have a map of structs. Why can't I directly modify a field in a struct value? but as I said, I can't operate with pointers (or?), therefor please don't mark this as duplicate as it is not!

If someone has an idea on how to do this right, please help!

12
  • a.B is map as such you can not reference a field of it. If you want to access a field of an object in the map you will need to use a key like you do above a.B["two"].A.B = a.CB. If you want to do this to every member of the map you should use a loop Commented May 7, 2018 at 19:59
  • Thank you, Benjamin! I've corrected the code in the question as this was a typo in writing this simple code. Still need help! Commented May 7, 2018 at 20:08
  • Where are you getting the error? a.C.A = "Test A (Epyt)" is definitely wrong, but doesn't have anything to do with maps. Commented May 7, 2018 at 20:19
  • @BoštjanBele What makes you think you cannot unmarshal into a map of pointers? Did you try it? Commented May 7, 2018 at 20:21
  • 1
    This is a duplicate of stackoverflow.com/questions/32751537/… and others. These other questions can also be made to work by using pointers. There is not a bug to correct in Go. Map values are not addressable. Commented May 7, 2018 at 20:21

2 Answers 2

1

I played around a bit and got it working! I thought there is an error, because if I use printf over the whole structure, I get the address instead of the value back. Sorry for the inconvenience!

As @mkopriva told me, I tried to use pointers. After some errors I got it to work so far! Sorted out this brain bug ;)

Thank you again!

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

Comments

1

You haven't initialised the map.

Try

var a Something
a.B = make(map[string]Type)

Also, none of the structures you're referring to exist.

Ie, instead of:

a.B["one"].A.A = ...

You should do:

a.B["one"] = Type{
    A: Epyt{
        A: "test",
        B: "foo",
    },
    B: "something",
}

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.