Newbie Question: I am confused by type declaration / type definitions

Just started fiddling with Go in earnest this past week. I have been reading the docs at The Go Programming Language Specification - The Go Programming Language (in particular) and am a bit confused so here are my questions:
when would I do the the following
A)

type (
  Foo int
  Bar string
)

versus

B)

type (
  Foo1 = int
  Bar1 = []string
)

Would I choose one style or the other, do these two code snippets really do the same thing.

Which is type declaration and which is type definition? I found the cited url anchor confusing and jumbled relative to the EBNF as well as what use cases might apply in the two forms identified above.

My confusion around declaring and defining types is furthered by a third type of example

type Foo struct {
  member1 int
  member2 int
  ...
}

This last variation looks the most familiar to me (first programming language I learned was C) and in this case it seems to me clear that we are declaring a particular structure as “type Foo.”

Any and all help in gluing together what I must be missing from my start with the docs is greatly appreciated. Most everything else language spec seems straightforward to me.

Thanks in advance,
Steve

You want he third variant for declaring struct types.

Neither of the first two are valid syntax. If you replace “type” with “struct” the first becomes an anonymous struct type - you might use this to declare a one off variable of the type in question without giving the the type a name, but it’s unusual. The middle one is a mix of struct syntax and type aliases (“type foo = bar”). Please forget that you saw something about equal signs in type declarations until much, much later in your Go career. :slight_smile:

Oh, and check out tour.golang.org which walks you through this. Welcome to Go and the forum.


Edit: I can’t read properly.

@calmh - attention, the first two examples have no curlies but regular (round) parentheses.

@Stephan_Doliov - The first one simply defines two types whereas the second one defines two type aliases.

In almost all cases you will want to use the first variant - a standard type declaration. It has some useful features, like allowing to define “incompatible” ints (think “Celsius” vs “Fahrenheit”) without risking to inadvertently assign a Celsius value to a Fahrenheit variable (in which case the compiler errors out).

The second variant, the type alias, was introduced to facilitate migrating code to a different package. Migrating functions, variables, or constants from package “A” to package “B” without immediately breaking package clients is no problem, but migrating types is. A type alias lets you define oldpackage.Celsius as an alias of newpackage.Celsius, and the package clients that still use the old package would see no difference. Now you can gradually fix all client code to use newpackage instead of oldpackage.

See this article for a more detailed introduction to type aliases.

2 Likes

Doh!

@Stephan_Doliov Sorry, and ignore everything I said except for maybe the last sentence.

Thanks to both of you @christophberger and @calmh. I think I have a handle
on it now re the difference between simple declarations and aliasing. I
also think the documentation at the original link I posted,
https://golang.org/ref/spec#Type_declarations could be improved a bit in
light of what I have just learned. Do either of you or anyone on this list
know whom (or what mailing list/forum etc.) I would reach out to in order
to propose some changes to that section of the docs?

Best,
Steve

You can send an issue to Go Repo if the issue is not already there.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.