13

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Yuval Meshorer
  • 438
  • 3
  • 9

4 Answers4

12

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}.

icza
  • 389,944
  • 63
  • 907
  • 827
2

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

The only option is to do it manually, as you have done. Whether you wrap that in a function or not, is a matter of taste/circumstance.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • Many people (such as myself) consider it a good thing. Conversion between arbitrary structs has arbitrary complexity. Having some "shortcut" way to do it would hide that complexity, which goes against one of Go's core philosophies of not hiding complexity from the coder. – Jonathan Hall Sep 19 '19 at 08:55
2

you can do something like this:

package main

import (
    "fmt"
)

type Big struct {
    Small
    C float32
    D byte
}

type Small struct {
    A int
    B string
}

func main() {
    big := new(Big)
    big.A = 1
    big.B = "test"
    big.C = 2.3
    fmt.Printf("big struct: %+v", big)
    fmt.Println()
    small := big.Small
    fmt.Printf("small struct: %+v", small)
    fmt.Println()
}

output:

big struct: &{Small:{A:1 B:test} C:2.3 D:0}
small struct: {A:1 B:test}

playgroundlink:https://play.golang.org/p/-jP8Wb--att

negi Yogi
  • 2,138
  • 2
  • 15
  • 20
1

I'm afraid there is no direct way to do that. What you did is the right way.

You can try to write the first object to JSON and then try to parse it back to the second object. Though, I wouldn't go this way.

One more way, which is a specific one is that the Big object will inherit the Small object. then you can downcast. Again, I wouldn't do that but if you must...

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94