Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang : Is conversion between different struct types possible?

Tags:

struct

go

Let's say I have two similar types set this way :

type type1 []struct {     Field1 string     Field2 int } type type2 []struct {     Field1 string     Field2 int } 

Is there a direct way to write values from a type1 to a type2, knowing that they have the same fields ? (other than writing a loop that will copy all the fields from the source to the target)

Thanks.

like image 521
Nicolas Marshall Avatar asked Jul 07 '14 14:07

Nicolas Marshall


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.

Does Go support type conversion?

Golang Implicit Type CastingGolang does not support implicit type conversion because of its robust type system. Some languages allow or even require compilers to provide coercion.

Is struct value type in Golang?

Structs are value types. When you assign one struct variable to another, a new copy of the struct is created and assigned. Similarly, when you pass a struct to another function, the function gets its own copy of the struct .

Which kind of conversion is supported by Golang programming?

Type conversion happens when we assign the value of one data type to another. Statically typed languages like C/C++, Java, provide the support for Implicit Type Conversion but Golang is different, as it doesn't support the Automatic Type Conversion or Implicit Type Conversion even if the data types are compatible.


Video Answer


2 Answers

To give a reference to OneOfOne's answer, see the Conversions section of the spec.

It states that

A non-constant value x can be converted to type T in any of these cases:

  • x is assignable to T.
  • x's type and T have identical underlying types.
  • x's type and T are unnamed pointer types and their pointer base types have identical underlying types.
  • x's type and T are both integer or floating point types.
  • x's type and T are both complex types.
  • x is an integer or a slice of bytes or runes and T is a string type.
  • x is a string and T is a slice of bytes or runes.

The first and highlighted case is your case. Both types have the underlying type

[]struct { Field1 string Field2 int } 

An underlying type is defined as

If T is one of the predeclared boolean, numeric, or string types, or a type literal, the corresponding underlying type is T itself. Otherwise, T's underlying type is the underlying type of the type to which T refers in its type declaration. (spec, Types)

You are using a type literal to define your type so this type literal is your underlying type.

like image 128
nemo Avatar answered Oct 05 '22 20:10

nemo


For your specific example, you can easily convert it playground:

t1 := type1{{"A", 1}, {"B", 2}} t2 := type2(t1) fmt.Println(t2) 
like image 24
OneOfOne Avatar answered Oct 05 '22 20:10

OneOfOne