1 min read
Circular generic types
There’s a lot to cover within type definitions! We’re several days in, and still going strong… Type definitions … In a type definition the given type cannot be a type parameter. type T[P any] P // illegal: P is a type parameter func f[T any]() { type L T // illegal: T is a type parameter declared by the enclosing function } I expect this will surprise noone. We’ve already seen that struct, array, and slice types cannot reference themselves.
1 min read
Definitions of generic types
Type definitions … If the type definition specifies type parameters, the type name denotes a generic type. Generic types must be instantiated when they are used. type List[T any] struct { next *List[T] value T } We haven’t covered instantiation yet, but we will. For now, let’s just summarize: Instantiation is process by which a generic type’s type parameters are substituted with the actual type arguments to be used.
2 min read
Methods aren't for "objects"
I’ll be livestreaming again today, in just a few minutes. Join me live, or watch the replay. I’ll be picking up where I left off, with my new linter project. One of the most powerful things I like about Go, is the ability to put methods on any type. Type definitions … Type definitions may be used to define different boolean, numeric, or string types and associate methods with them:
2 min read
Types with methods
Type definitions … A defined type may have methods associated with it. It does not inherit any methods bound to the given type, but the method set of an interface type or of elements of a composite type remains unchanged: // A Mutex is a data type with two methods, Lock and Unlock. type Mutex struct { /* Mutex fields */ } func (m *Mutex) Lock() { /* Lock implementation */ } func (m *Mutex) Unlock() { /* Unlock implementation */ } // NewMutex has the same composition as Mutex but its method set is empty.
Subscribe to Boldly Go: Daily
Every day I'll send you advice to improve your understanding of Go. Don't miss out! I will respect your inbox, and honor my privacy policy.
Unsure? Browse the archive.
1 min read
Type definitions
Some of the minutiae we’ve gone over about underlying types, and type identity are finally starting to come together… Type definitions A type definition creates a new, distinct type with the same underlying type and operations as the given type and binds an identifier, the type name, to it. TypeDef = identifier [ TypeParameters ] Type . The new type is called a defined type. It is different from any other type, including the type it is created from.
40 min listen
Writing Go and doin' DevOps on Backend Banter
Listen as I discuss Go, DevOps, and more, with Lane Wagner on the Backend Banter podcast.
2 min read
Alias declarations
Alias declarations An alias declaration binds an identifier to the given type. AliasDecl = identifier "=" Type . Within the scope of the identifier, it serves as an alias for the type. type ( nodeList = []*Node // nodeList and []*Node are identical types Polar = polar // Polar and polar denote identical types ) Aliases are a feature you likely won’t use frequently. You may never use it, in fact.
1 min read
Type declarations
We’ve already talked about the different types availabe in Go, but now we’ll see how to declare them… just in case it wasn’t already obvious. Type declarations A type declaration binds an identifier, the type name, to a type. Type declarations come in two forms: alias declarations and type definitions. TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) . TypeSpec = AliasDecl | TypeDef . We’ll talk about these two different forms in the coming days.
2 min read
Iota, continued
No live stream today. My office/recording studio is being painted. I’ll be back next week with more linter building! One last detail on iota… Iota … By definition, multiple uses of iota in the same ConstSpec all have the same value: const ( bit0, mask0 = 1 << iota, 1<<iota - 1 // bit0 == 1, mask0 == 0 (iota == 0) bit1, mask1 // bit1 == 2, mask1 == 1 (iota == 1) _, _ // (iota == 2, unused) bit3, mask3 // bit3 == 8, mask3 == 7 (iota == 3) ) This last example exploits the [implicit repetition](https://go.
2 min read
Iota
You may recall from yesterday that you can omit the constant expression for all but the first ConstantSpec in a parenthesized constant declaration, but that it wasn’t very useful by itself. Enter iota… Iota Within a constant declaration, the predeclared identifier iota represents successive untyped integer constants. Its value is the index of the respective ConstSpec in that constant declaration, starting at zero. It can be used to construct a set of related constants:
2 min read
Omitting constant expressions
Constant declarations … Within a parenthesized const declaration list the expression list may be omitted from any but the first ConstSpec. Such an empty list is equivalent to the textual substitution of the first preceding non-empty expression list and its type if any. Omitting the list of expressions is therefore equivalent to repeating the previous list. The number of identifiers must be equal to the number of expressions in the previous list.