根据过滤条件去lang构造地图

I have below structures

type ModuleList []Module

type Module struct {
    Id                  string
    Items               []Item
    Env                 map[string]string
}

type Item struct {
    Id    string
    Host  string
}

I have a service which returns ModuleList; but I would like to create a function which can group ModuleList based on Module Env key value and return map[string]ModuleList or map[string]*Module

Can i have any sample function which does this ?

I had tried doing this

appsByGroup := make(map[string]ModuleList)
    for _, app := range apps {
        if _, ok := app.Env["APP_GROUP"]; ok {
            appGroup := app.Env["APP_GROUP"]
            appsByGroup[appGroup] = app
        }
    }

; but not quite sure how to add element to an array

If you want to group all the Modules by a APP_GROUP then you are pretty much correct. You are just not appending to the slice correctly:

appsByGroup := make(map[string]ModuleList)
for _, app := range apps {
    if _, ok := app.Env["APP_GROUP"]; ok {
        appGroup := app.Env["APP_GROUP"]
        // app is of type Module while in appsGroup map, each string
        // maps to a ModuleList, which is a slice of Modules.
        // Hence your original line
        // appsByGroup[appGroup] = app would not compile
        appsByGroup[appGroup] = append(appsByGroup[appGroup], app)
    }
}

Now you can access all the Modules (stored in a slice) in a group by using:

// returns slice of modules (as ModuleList) with
// Env["APP_GROUP"] == group
appGroups[group]