使用其他属性作为键从结构创建地图或对象

i've struct which build like following

type RTB struct {
    ID            string
    Modules       []*Modules   
    Req            []*Req
}

Now inside module I've the following

type Modules struct {
    Name       string
    Type       string
    Path       string
    Id         string
}

Now I've the object of RTB in the memory and I want to create map (that I can loop on it si object which will be like following:

   NewObject {
        Type          string//the value from the module struct
        Modules       []*Modules // From the rtb struct  
    }

of course I can loop on it (if there is not more elegant way...) and create new struct (like the new object) and fill the data from both structs, but there is better way in Golang like map to store this data?

You have to use a loop to go over the modules and build a map from it. No easier way. If you need this functionality in multiple places, put it into a utility function and call that wherever needed.

Example building the map:

rtb := &RTB{
    Modules: []*Modules{
        {Name: "n1", Type: "t1"},
        {Name: "n2", Type: "t1"},
        {Name: "n3", Type: "t2"},
    },
}

m := map[string][]*Modules{}

for _, mod := range rtb.Modules {
    m[mod.Type] = append(m[mod.Type], mod)
}

// Verify result (JSON for "nice" outpout):
fmt.Println(json.NewEncoder(os.Stdout).Encode(m))

Output:

{"t1":[{"Name":"n1","Type":"t1"},{"Name":"n2","Type":"t1"}],"t2":[{"Name":"n3","Type":"t2"}]}
<nil>

If you want a slice of NewObject instead of the map, you can build that like this:

// m is the map built above.

var newObjs []*NewObject
for k, v := range m {
    newObjs = append(newObjs, &NewObject{
        Type:    k,
        Modules: v,
    })
}

fmt.Println(json.NewEncoder(os.Stdout).Encode(newObjs))

Output:

[{"Type":"t1","Modules":[{"Name":"n1","Type":"t1"},{"Name":"n2","Type":"t1"}]},{"Type":"t2","Modules":[{"Name":"n3","Type":"t2"}]}]
<nil>

Try the examples on the Go Playground.