0

For basic types we can easily cast the types if their underlying types are same .But the fields inside struct with same memory layout cannot be easily cast from one to another type. There is a proposal for this problem unfortunately it got rejected .After an hour of googling with no luck I came here seeking the help from experts.

Look at example below :

package main

import (
    "fmt"
)

type Int int

type A struct {
    name string
    age  Int
}
type B struct {
    name string
    age  int
}

func main() {
    var a A= A{"Foo",21}
    var b B= B{"Bar", 21}
    fmt.Println(a,b,(A)(b))  //Error here as expected
}

Eventhough struct A and B has the same underlying type of struct { string,int} why cannot I cast to each other as the underlying type of Int is int. Whether it is possible to cast recursively unless the underlying type differs?

3
  • 1
    There are no type casts in Go. You are talking about type conversions and this type of type conversion is simply dissalowed.
    – Volker
    Dec 19, 2020 at 8:45
  • sorry for that.I am from oops background,that causes issues in naming conventions. Dec 19, 2020 at 9:20
  • 1
    "Type casting" is not an OOP concept at all. In fact, it predates OOP by decades. It's also not a matter of naming conventions. It's a matter of functionality. "Type casing" describes a very specific behavior that Go does not support. Dec 19, 2020 at 9:53

1 Answer 1

1

You can't do it simply because the language spec does not allow it. Regarding structs, you can only convert from one type to the other if Spec: Conversion:

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

If you're absolutely sure the structs' memory layout is identical, you may use an unsafe conversion (using package unsafe) like this:

var a A = A{"Foo", 21}
var b B

b = *(*B)(unsafe.Pointer(&a))
fmt.Println(a, b)

This will output (try it on the Go Playground):

{Foo 21} {Foo 21}

But use this as the last resort. Using unsafe you lose compile time type safety and portability guarantees. E.g. if later you only modify one of the structs, the above code will continue to compile even though it might not be correct anymore, and the compiler will not able to inform you about that.

2
  • What about two maps which are similar to above case ie) map[int][string] and map[Int][string] ? Dec 19, 2020 at 8:38
  • 1
    @chellathurai Maps are pointers in Go. How the values are hashed and used in its internal structure is implementation detail. Don't try to utilize this unsafe conversion. Try to avoid it.
    – icza
    Dec 19, 2020 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.