Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a struct to a different struct with fewer fields

Tags:

struct

go

I am trying to copy a struct of type Big to type Small without explicitly creating a new struct of type Small with the same fields.

I have tried searching for other similar problems such as this and this yet all the conversions between different struct types happen only if the structs have the same fields.

Here is an example of what I tried to do:

// Big has all the fields that Small has including some new ones.
type Big struct {
    A int
    B string
    C float
    D byte
}

type Small struct {
    A int
    B string
}

// This is the current solution which I hope to not use.
func ConvertFromBigToSmall(big Big) Small {
    return Small{
        A: big.A,
        B: big.B,
    }
}

I expected to be able to do something like this, yet it does not work:

big := Big{}
small := Small(big)

Is there a way of converting between Big to Small (and maybe even vice-versa) without using a Convert function?

like image 962
Yuval Meshorer Avatar asked Sep 19 '19 08:09

Yuval Meshorer


People also ask

How do you assign one structure to another structure in Golang?

A struct variable in Golang can be copied to another variable easily using the assignment statement(=). Any changes made to the second struct will not be reflected back to the first struct.

What is a go struct?

A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct.

What is Type Golang?

Khizar Hayat Saani. Type is the base interface for all data types in Go. This means that all other data types (such as int , float , or string ) implement the Type interface. Type is defined in the reflect header.

How do you copy an object in Golang?

If a and aa are both value types (not pointers), then you're already creating a copy. If they're not, dereference in the assignment to create a copy: aa := *a . The addresses are different, but assert compares the values pointed to.


1 Answers

There is no built-in support for this. If you really need this, you could write a general function which uses reflection to copy the fields.

Or you could redesign. If Big is a Small plus some other, additional fields, why not reuse Small in Big?

type Small struct {
    A int
    B string
}

type Big struct {
    S Small
    C float
    D byte
}

Then if you have a Big struct, you also have a Small: Big.S. If you have a Small and you need a Big: Big{S: small}.

If you worry about losing the convenience of shorter field names, or different marshalled results, then use embedding instead of a named field:

type Big struct {
    Small // Embedding
    C float
    D byte
}

Then these are also valid: Big.A, Big.B. But if you need a Small value, you can refer to the embedded field using the unqualified type name as the field name, e.g. Big.Small (see Golang embedded struct type). Similarly, to create a Big from a Small: Big{Small: small}.

like image 116
icza Avatar answered Oct 19 '22 17:10

icza