I have a struct:
type Person struct {
FirstName string
LastName int
Age int
HairColor string
EyeColor string
Height string
}
And I have a function that takes in 2 parameters, and updates the first Person's fields to the second person's fields:
func updateFields(personA *Person, personB Person) {
personA.FirstName = personB.FirstName
personA.LastName = personB.LastName
// Don't want to change Age.
personA.HairColor = personB.HairColor
personA.EyeColor = personB.EyeColor
personA.Height = personB.Height
}
Instead of hard-coding the values to change, how can I loop through the fields and update the first Person to have the same values as the second Person except for the "Age" field?
You can just copy the entire personB into personA and make a backup of the Age field before:
func updateFields(personA *Person, personB Person) {
tempAge := personA.Age
*personA = personB
personA.Age = tempAge
}
For trivially copying every field, you can simply do something like *personA = personB. If you need to "not copy" just one specific field (same field every time), you could probably just save that field's value in a separate variable, copy everything with *personA = personB, and then copy the value back. But this is only useful for very specific situations. It wouldn't allow for example to have a dynamic set of fields not to copy.
If you want to do it with more flexibility, you can do it using reflection, sample code below.
Notice that there may be a few limitations. Notably, you can only set exported fields. Also, if you don't test for these limitations and accidentally try to set a field that's not settable, or with a value of type that's not assignable to that field, etc, the reflect package will happily panic. So it's wise to add a lot of checks before you actually .Set(...) the field.
import (
"fmt"
"reflect"
)
type Person struct {
FirstName string
LastName int
Age int
HairColor string
EyeColor string
Height string
}
func updateFields(personA *Person, personB Person) {
// .Elem() called to dereference the pointer
aVal := reflect.ValueOf(personA).Elem()
aTyp := aVal.Type()
// no .Elem() called here because it's not a pointer
bVal := reflect.ValueOf(personB)
for i := 0; i < aVal.NumField(); i++ {
// skip the "Age" field:
if aTyp.Field(i).Name == "Age" {
continue
}
// you might want to add some checks here,
// eg stuff like .CanSet(), to avoid panics
aVal.Field(i).Set(bVal.Field(i))
}
}
func main() {
b := Person{
FirstName: "Bruno",
LastName: 1,
Age: 2,
HairColor: "hello",
EyeColor: "world",
Height: "tall",
}
a := Person{}
fmt.Println(a)
updateFields(&a, b)
fmt.Println(a)
}
Related
I have an Employee struct which looks like this:
type Employee struct {
firstName string
lastName string
role string
}
I read from an Excel file a row, which has the corresponding columns in the same order.
From the excel file I created an array that holds this data, so the array looks like this:
[personFirstName, personLastName, personRole]
And I want to initialize my Employee struct with it.
But I can't figure out how, because I can't iterate over the Employee struct like it's an array.
I thought about doing it manually, like this:
e := {
firstName: array[0],
lastName: array[1],
role: array[2],
}
But I guess there should be a better way.
Any Ideas?
There's nothing wrong with the code in the question. Here's how to use the reflect API to accomplish the goal:
Export the field to make the fields settable through the reflect API.
type Employee struct {
FirstName string
LastName string
Role string
}
Create reflect value for an employee. The & and Elem()
shenanigans are required to get a settable value.
var e Employee
v := reflect.ValueOf(&e).Elem()
For each field, set the string:
for i := 0; i < v.NumField(); i++ {
v.Field(i).SetString(values[i])
}
Run an example on the Go Playground.
Put the code in a function if you need it in more than one place:
// setStrings sets the fields of the struct pointed
// to by ptr to the specified values.
func setStrings(ptr interface{}, values []string) {
v := reflect.ValueOf(ptr).Elem()
for i := 0; i < v.NumField(); i++ {
v.Field(i).SetString(values[i])
}
}
Call it like this:
values := []string{"Monica", "Smith", "Manager"}
var e Employee
setStrings(&e, values)
fmt.Printf("%+v\n", e) // prints {FirstName:Monica LastName:Smith Role:Manager}
Run an example on the playground.
I am really new to Go, so want some advice.
I have a struct:
type Employee struct {
Name string
Designation string
Department string
Salary int
Email string
}
I want to concatenate the string fields into a type of employee description.
So that, I can say:
toString(employee) and get:
John Smith Manager Sales john.smith#example.com
I tried to fetch each field, check if they are empty and put them in a slice and join them at the end
employeeDescArr := make([]string, 0, 4)
if strings.TrimSpace(value) != "" {
append(employee.GetName(), value)
}...
return strings.Join(employeeDescArr[:], " ")
I think this method is very verbose and shows lack of Go skills.
Is it better to use a string Builder instead?
Is there a way to iterate through all fields of a struct in a Reflection way and join them?
Loop through the string fields and collect non-empty strings. Join the fields.
func (e *Employee) String() string {
var parts []string
for _, s := range []string{e.Name, e.Designation, e.Department, e.Email} {
if strings.TrimSpace(s) != "" {
parts = append(parts, s)
}
}
return strings.Join(parts, " ")
}
Because the strings.Join function is implemented using strings.Builder, there's no benefit to replacing strings.Join with application code that uses strings.Builder.
Here's how to use reflect to avoid listing the fields in the string function:
var stringType = reflect.TypeOf("")
func (e *Employee) String() string {
v := reflect.ValueOf(e).Elem()
var parts []string
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
if f.Type() == stringType {
s := f.String()
if strings.TrimSpace(s) != "" {
parts = append(parts, s)
}
}
}
return strings.Join(parts, " ")
}
If you want to include all fields (include non-strings and empty strings), then you can fmt.Sprint(e) to get a string. See https://play.golang.org/p/yntZxQ-Xs6C.
You can make it less verbose by writing an utility function to make the addition with the "non-blank-string" check.
Also, you could make your type implement implement an String() method which has the advantage it will make it print as you wish when used in fmt print functions.
This addToString function is generic so you could reuse it if doing this for other types:
func addToString(original string, addition interface{}) string {
additionStr := fmt.Sprint(addition)
if additionStr != "" {
if original != "" {
original += " "
}
original += additionStr
}
return original
}
Then you can implement it like this, which is not so verbose:
type Employee struct {
Name string
Designation string
Department string
Salary int
Email string
}
func (e *Employee) String() string {
theString := ""
theString = addToString(theString, e.Name)
theString = addToString(theString, e.Designation)
theString = addToString(theString, e.Department)
theString = addToString(theString, e.Salary)
theString = addToString(theString, e.Email)
return theString
}
And use it like this:
func main() {
emp := &Employee{
Name: "Jonh",
Department: "Some dept",
}
fmt.Println(emp.String())
fmt.Println(emp)
}
Which will output:
Jonh Some dept 0
Jonh Some dept 0
I think you would want to use the Stringer interface instead. ie:
package main
import (
"fmt"
"strings"
"strconv"
)
type Employee struct {
Name string
Designation string
Department string
Salary int
Email string
}
func main() {
emp1:=Employee{Name:"Cetin", Department:"MS", Salary:50}
emp2:=Employee{Name:"David", Designation:"Designation", Email:"david#nowhere.com"}
emp3:=Employee{Department:"Space", Salary:10}
fmt.Println(emp1)
fmt.Println(emp2)
fmt.Println(emp3)
}
func (e Employee) String() string {
var salary string
if e.Salary > 0 {
salary = strconv.Itoa(e.Salary) + " "
} else {
salary = ""
}
return strings.TrimSpace(
strings.TrimSpace(
strings.TrimSpace(e.Name + " " + e.Designation) + " " +
e.Department) + " " +
salary +
e.Email)
}
Playground: https://play.golang.org/p/L8ft7SeXpqt
PS: I later noticed you only want string fields, but didn't remove salary anyway.
Package fmt
import "fmt"
type Stringer
Stringer is implemented by any value that has a String method, which
defines the “native” format for that value. The String method is used
to print values passed as an operand to any format that accepts a
string or to an unformatted printer such as Print.
type Stringer interface {
String() string
}
Write a String method for type Employee.
For example,
package main
import (
"fmt"
"strings"
)
type Employee struct {
Name string
Designation string
Department string
Salary int
Email string
}
func appendItem(items *strings.Builder, item string) {
if len(item) > 0 {
if items.Len() > 0 {
items.WriteByte(' ')
}
items.WriteString(item)
}
}
func (e Employee) String() string {
s := new(strings.Builder)
appendItem(s, e.Name)
appendItem(s, e.Designation)
appendItem(s, e.Department)
appendItem(s, e.Email)
return s.String()
}
func main() {
ee := Employee{
Name: "John Smith",
Designation: "Manager",
Department: "Sales",
Email: "john.smith#example.com",
Salary: 42000,
}
fmt.Println(ee)
}
Playground: https://play.golang.org/p/EPBjgi8usJ-
Output:
John Smith Manager Sales john.smith#example.com
I want to concatenate the string fields into a type of employee
description.
Is there a way to iterate through all fields of a struct in a
Reflection way and join them?
[Reflection is] a powerful tool that should be used with care and avoided unless strictly necessary. Rob Pike
The Go Blog: The Laws of Reflection
Reflection is never clear. Rob Pike
Go Proverbs - Rob Pike - Gopherfest - November 18, 2015
Go compiled code is efficient. The Go reflect package functions are interpreted at run time.
Iterating through all fields of a struct has the same bug as SELECT * FROM table; in SQL. The values returned are determined at run time, not compile time.
If your case, The business requirement is to hide confidential fields, like salary, and limit the fields displayed to a few key descriptive fields. Inevitably, fields will be added to the struct. The "concatenate the string fields" specification is unlikely to be correct now or in the future.
I came across this situation where I was trying to assign values to a struct within a struct. There is no compiler error, but it does panic when you run it. Does Go have a different way to handle this data structure?
package main
import (
"fmt"
)
type Label struct {
ID int
Labels []struct {
ID int
Name string
}
}
func main() {
l := Label{}
l.ID = 100
l.Labels[0].ID = 200
l.Labels[0].Name = "me"
fmt.Println(l.ID)
fmt.Println(l.Labels[0].ID)
fmt.Println(l.Labels[0].Name)
}
https://play.golang.org/p/IiuXpaDvF1W
Thanks in advance.
The default value for a slice is nil so it has no elements, and you cannot assign to index 0 because it doesn't exist yet.
You can use append to add a new element to that slice using:
l.Labels = append(l.Labels, struct{
ID int
Name string
}{
ID: 200,
Name: "me",
})
https://play.golang.org/p/uAWdQdh0Ov7
In addition, your use of an inline/anonymous struct here means you'll need to redeclare the type when you append. Consider adding another declared type:
type SubLabel struct {
ID int
Name string
}
type Label struct {
ID int
Labels []SubLabel
}
// ...
l.Labels = append(l.Labels, SubLabel{
ID: 200,
Name: "me",
})
https://play.golang.org/p/4idibGH6Wzd
I have the following structs...
type Menu struct {
Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"`
Description string `protobuf:"bytes,3,opt,name=description" json:"description,omitempty"`
Mixers []*Mixer `protobuf:"bytes,4,rep,name=mixers" json:"mixers,omitempty"`
Sections []*Section `protobuf:"bytes,5,rep,name=sections" json:"sections,omitempty"`
}
And...
type Menu struct {
ID bson.ObjectId `json:"id" bson:"_id"`
Name string `json:"name" bson:"name"`
Description string `json:"description" bson:"description"`
Mixers []Mixer `json:"mixers" bson:"mixers"`
Sections []Section `json:"sections" bson:"sections"`
}
I basically need to convert between the two struct types, I've attempted to use mergo, but that can only merge structs that are assignable to one another. The only solution I have so far is iterating through each struct, converting the ID by re-assigning it and converting its type between string and bson.ObjectId. Then iterating through each map field and doing the same. Which feels like an inefficient solution.
So I'm attempting to use reflection to be more generic in converting between the two ID's. But I can't figure out how I can effectively merge all of the other fields that do match automatically, so I can just worry about converting between the ID types.
Here's the code I have so far...
package main
import (
"fmt"
"reflect"
"gopkg.in/mgo.v2/bson"
)
type Sub struct {
Id bson.ObjectId
}
type PbSub struct {
Id string
}
type PbMenu struct {
Id string
Subs []PbSub
}
type Menu struct {
Id bson.ObjectId
Subs []Sub
}
func main() {
pbMenus := []*PbMenu{
&PbMenu{"1", []PbSub{PbSub{"1"}}},
&PbMenu{"2", []PbSub{PbSub{"1"}}},
&PbMenu{"3", []PbSub{PbSub{"1"}}},
}
newMenus := Serialise(pbMenus)
fmt.Println(newMenus)
}
type union struct {
PbMenu
Menu
}
func Serialise(menus []*PbMenu) []Menu {
newMenus := []Menu{}
for _, v := range menus {
m := reflect.TypeOf(*v)
fmt.Println(m)
length := m.NumField()
for i := 0; i < length; i++ {
field := reflect.TypeOf(v).Field(i)
fmt.Println(field.Type.Kind())
if field.Type.Kind() == reflect.Map {
fmt.Println("is map")
}
if field.Name == "Id" && field.Type.String() == "string" {
// Convert ID type
id := bson.ObjectId(v.Id)
var dst Menu
dst.Id = id
// Need to merge other matching struct fields
newMenus = append(newMenus, dst)
}
}
}
return newMenus
}
I'm can't just manually re-assign the fields because I'm hoping to detect maps on the structs fields and recursively perform this function on them, but the fields won't be the same on embedded structs.
Hope this makes sense!
I think that it is probably better to write your own converter, because you will always have some cases that are not covered by existing libs\tools for that.
My initial implementation of it would be something like this: basic impl of structs merger
I have two struct having the same members, I want to copy one struct to another, see the pseudo code below:
type Common struct {
Gender int
From string
To string
}
type Foo struct {
Id string
Name string
Extra Common
}
type Bar struct {
Id string
Name string
Extra Common
}
Then I have foo of struct Foo, and bar of struct Bar, Is there any way to copy bar from foo?
Use a conversion to change the type. The following code uses a conversion to copy a value of type Foo to a value of type Bar:
foo := Foo{Id: "123", Name: "Joe"}
bar := Bar(foo)
playground example
The conversion only works when the underlying types are identical except for struct tags.
https://github.com/jinzhu/copier (same author of gorm) is also quite a good one, I have nested structs and all I do is:
copier.Copy(&employees, &user)
works great
If you would like to copy or clone to a different struct, I would suggest using deepcopier
It provides nice features like skipping, custom mapping, and forcing. below is an example from github:
Install:
go get -u github.com/ulule/deepcopier
Example:
package main
import (
"fmt"
"github.com/ulule/deepcopier"
)
// Model
type User struct {
// Basic string field
Name string
// Deepcopier supports https://golang.org/pkg/database/sql/driver/#Valuer
Email sql.NullString
}
func (u *User) MethodThatTakesContext(ctx map[string]interface{}) string {
// do whatever you want
return "hello from this method"
}
// Resource
type UserResource struct {
//copy from field "Name"
DisplayName string `deepcopier:"field:Name"`
//this will be skipped in copy
SkipMe string `deepcopier:"skip"`
//this should call method named MethodThatTakesContext
MethodThatTakesContext string `deepcopier:"context"`
Email string `deepcopier:"force"`
}
func main() {
user := &User{
Name: "gilles",
Email: sql.NullString{
Valid: true,
String: "gilles#example.com",
},
}
resource := &UserResource{}
deepcopier.Copy(user).To(resource)
//copied from User's Name field
fmt.Println(resource.DisplayName)//output: gilles
fmt.Println(resource.Email) //output: gilles#example.com
fmt.Println(resource.MethodThatTakesContext) //output: hello from this method
}
Also, some other way you could achieve this is by encoding the source object to JSON and then decode it back to the destination object.
This is another possible answer
type Common struct {
Gender int
From string
To string
}
type Foo struct {
Id string
Name string
Extra Common
}
type Bar struct {
Id string
Name string
Extra Common
}
foo:=Foo{
Id:"123",
Name:"damitha",
Extra: struct {
Gender int
From string
To string
}{Gender:1 , From:"xx", To:"yy" },
}
bar:=*(*Bar)(unsafe.Pointer(&foo))
fmt.Printf("%+v\n",bar)
If you would like to copy or clone to a different struct, I would suggest using deepcopier
It provides nice features like skipping, custom mapping, and forcing.
You can achieve nested struct copy following way.
Install:
go get -u github.com/ulule/deepcopier
Example:
package main
import (
"fmt"
"strconv"
"github.com/ulule/deepcopier"
)
//FieldStruct - Field Struct
type FieldStruct struct {
Name string `deepcopier:"field:TargetName"`
Type string `deepcopier:"field:TargetType"`
}
//SourceStruct - Source Struct
type SourceStruct struct {
Name string `deepcopier:"field:TargetName"`
Age int `deepcopier:"field:TargetAge"`
StringArray []string `deepcopier:"field:TargetStringArray"`
StringToInt string `deepcopier:"context"`
Field FieldStruct
Fields []FieldStruct
}
//TargetFieldStruct - Field Struct
type TargetFieldStruct struct {
TargetName string
TargetType string
}
//TargetStruct - Target Struct
type TargetStruct struct {
TargetName string
TargetAge int
TargetStringArray []string
TargetInt int
TargetField TargetFieldStruct
TargetFields []TargetFieldStruct
}
//write methods
//TargetInt - StringToInt
func (s *SourceStruct) TargetInt() int {
i, _ := strconv.Atoi(s.StringToInt)
return i
}
func main() {
s := &SourceStruct{
Name: "Name",
Age: 12,
StringArray: []string{"1", "2"},
StringToInt: "123",
Field: FieldStruct{
Name: "Field",
Type: "String",
},
Fields: []FieldStruct{
FieldStruct{
Name: "Field1",
Type: "String1",
},
FieldStruct{
Name: "Field2",
Type: "String2",
},
},
}
t := &TargetStruct{}
//coping data into inner struct
deepcopier.Copy(&t.TargetField).From(&s.Field)
// copied array of Struct
for i := range s.Fields {
// init a struct
t.TargetFields = append(t.TargetFields, TargetFieldStruct{})
// coping the data
deepcopier.Copy(&t.TargetFields[i]).From(&s.Fields[i])
}
//Top level copy
deepcopier.Copy(t).From(s)
fmt.Println(t)
}
Output:
&{Name 12 [1 2] 123 {Field String} [{Field1 String1} {Field2 String2}]}