How to append struct member dynamically using Empty Interface?
Example
package main
import (
"fmt"
)
type Before struct {
m string
}
func append(b interface{}) interface{} {
return struct {
Before
n string
}{b.(Before), "rest"}
}
func main() {
b := Before{"test"}
a := append(b)
fmt.Println(a)
}
{{test} rest}
Most Helpful This Week
Concurrently printing array elements using goroutines and channels
How do you catch panic in Golang?
How to check specific field exist in struct?
How to create Slice of Struct in Golang?
How to initialize a struct containing a slice of structs in Golang?
How to build a map of struct and append values to it?