DEV Community

Nitin Bansal
Nitin Bansal

Posted on

Simplest example to understand types, aliases and comparisons between them

#go

Have a look at this example:

func main() {
    type _a int32
    type _b _a
    type _c = _a

    var a _a = 10
    var b _b = 10
    var c _c = 10

    fmt.Println(a == _a(b))
    fmt.Println(a == c)
}
Enter fullscreen mode Exit fullscreen mode

What you see here is first fmt statement requiring a cast, whereas second one doesn't.

The reason for this is 2 simple rules:

  1. Types created from another type, even containing same exact same fields, are not directly comparable
  2. Aliases created from types are directly comparable.

For both: comparison after typecasting works perfectly well.

Sweet and simple..😊

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay