Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang dynamically creating member of struct

Tags:

go

go-server

I know there is struct in Go, but for all I know, you have to define struct

type Circle struct{
    x,y,r float64
}

I am wondering how you can declare a new variable that doesn't exist in the struct

circle := new(Circle)
circle.color = "black"
like image 284
UniSound Waterloo Avatar asked Nov 12 '16 03:11

UniSound Waterloo


People also ask

Can we create a dynamic struct in Golang?

Golang dynamic struct. Package dynamic struct provides possibility to dynamically, in runtime, extend or merge existing defined structs or to provide completely new struct. Main features: Building completely new struct in runtime.

What is go struct?

In Go programming, a structure or struct is a user-defined type to store a collection of different fields into a single field. For example, suppose you have a player and want to store his name and age. You can create two variables, name, and age, to store the values.


3 Answers

You will need to use a map (of type map[string]interface{}) to work with dynamic JSON. Here is an example of creating a new map:

// Initial declaration
m := map[string]interface{}{
    "key": "value",
}

// Dynamically add a sub-map
m["sub"] = map[string]interface{}{
    "deepKey": "deepValue",
}

Unmarshalling JSON into a map looks like:

var f interface{}
err := json.Unmarshal(b, &f)

The code above would leave you with a map in f, with a structure resembling:

f = map[string]interface{}{
    "Name": "Wednesday",
    "Age":  6,
    "Parents": []interface{}{
        "Gomez",
        "Morticia",
    },
}

You will need to use a type assertion to access it, otherwise Go won't know it's a map:

m := f.(map[string]interface{})

You will also need to use assertions or type switches on each item you pull out of the map. Dealing with unstructured JSON is a hassle.

More information:

  • https://blog.golang.org/json-and-go
  • https://godoc.org/encoding/json#Unmarshal
like image 100
James Hillyerd Avatar answered Oct 08 '22 23:10

James Hillyerd


I've started to work on this small repository https://github.com/Ompluscator/dynamic-struct

It's possible at this point to extend existing struct in runtime, by passing a instance of struct and modifying fields (adding, removing, changing types and tags).

Still in progress, so don't expect something huge :)

EDIT: At this point, work on library is done, and it looks stable for last a couple of months :)

like image 39
Marko Milojevic Avatar answered Oct 08 '22 22:10

Marko Milojevic


You can do it using reflect package, check StructOf method it allows you to create a new struct from []reflect.StructField. Example:

func main() {
typ := reflect.StructOf([]reflect.StructField{
    {
        Name: "Height",
        Type: reflect.TypeOf(float64(0)),
        Tag:  `json:"height"`,
    },
    {
        Name: "Age",
        Type: reflect.TypeOf(int(0)),
        Tag:  `json:"age"`,
    },
})

v := reflect.New(typ).Elem()
v.Field(0).SetFloat(0.4)
v.Field(1).SetInt(2)
s := v.Addr().Interface()

w := new(bytes.Buffer)
if err := json.NewEncoder(w).Encode(s); err != nil {
    panic(err)
}

fmt.Printf("value: %+v\n", s)
fmt.Printf("json:  %s", w.Bytes())

r := bytes.NewReader([]byte(`{"height":1.5,"age":10}`))
if err := json.NewDecoder(r).Decode(s); err != nil {
    panic(err)
}
fmt.Printf("value: %+v\n", s)

}

like image 36
b.b3rn4rd Avatar answered Oct 08 '22 22:10

b.b3rn4rd