Index expressions for maps

Index expressions … For a of map type M: x’s type must be assignable to the key type of M No surprise here. The key must be assignable to the key type. But notably, it needn’t be of an identical type, so long as it is assignable to the key type. m := map[string]int{ "foo": 3, "bar": 12, } m[string("foo")] // string("foo") is explicitly of type string, so assignable. m["foo"] // Untyped constant string "foo" is assignable to type `string` type Key string k := Key("bar") m[k] // k is of type Key, not assignable to type string without explicit conversion if the map contains an entry with key x, a[x] is the map element with key x and the type of a[x] is the element type of M This should be intuitive.

Index expressions for slices and strings

Index expressions … For a of slice type S: if x is out of range at run time, a run-time panic occurs a[x] is the slice element at index x and the type of a[x] is the element type of S This is essentially identical to the rules for arrays, except that there’s no possibility of a compile-time error when using a constant index, since the length of a slice is not known at compilation time.

Index expressions for arrays

I hope you can join me again today on my live stream, when I’ll be continuing my series about deploying a simple Go app to Kubernetes. Last week we started with index expressions. Today we look at the additional rules that apply specifically to index expressions for arrays. Index expressions … For a of array type A: a constant index must be in range Since the length of an array is known at compile time, it is a compilation error to use a constant index that is out of range.

Index expressions

Index expressions A primary expression of the form a[x] denotes the element of the array, pointer to array, slice, string or map a indexed by x. The value x is called the index or map key, respectively. The following rules apply: If a is neither a map nor a type parameter: Alright, so we’re looking at the rules that apply to any of the following types: array, pointer to array, slice, or string—except for type parameters of these types, which have special rules we’ll get to.

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

I'm looking for new clients

Could your project benefit from my expertise? I’ve had some availability open up in my schedule recently, so I’m announcing my new “Fractional Gopher” subscription service. I can help build your new features, pay down technical debt, audit your project’s code and architecture, or help in any number of ways. You’re already running my code in production. I’ve contributed to the Go standard library, as well as popular Go libraries such as echo, Logrus, and many others.

Method values, part 2

Today we finish up the spec’s section on method values, with some very non-surprising bits: Method values … As with selectors, a reference to a non-interface method with a value receiver using a pointer will automatically dereference that pointer: pt.Mv is equivalent to (*pt).Mv. As with method calls, a reference to a non-interface method with a pointer receiver using an addressable value will automatically take the address of that value: t.

Method values

A quick reminder: In today’s live stream, I’ll be deploying a Go app to Kubernetes! I hope you’ll join me! Today we continue the explanation of Method values. There’s nothing too surprising today. The spec mostly just confirms what should seem natural… Method values … As in the discussion of method expressions above, consider a struct type T with two methods, Mv, whose receiver is of type T, and Mp, whose receiver is of type *T.


1 min read

Let's do Kubernetes

I’m taking a small detour today, to a topic that’s not strictly Go-related, but which I get asked about a lot: Docker and Kubernetes. Starting on Monday, in what is likely to become a 2- or 3-week series, I’m going to be deploying a small Go app I wrote, to Kubernetes on Google Cloud. I’ve done this before, but it’s been a few years, so I’m a bit rusty. And Kubernetes has changed (as it always does), so there are sure to be some educational bumps along the way.


24 min watch

Resume advice for a first-time job seeker

I review the résumé/CV of a first-time job seeker, looking for a Go-related job.

Method values

As I promised yesterday, today we’re looking at method values, a concept with some similarties to the previous concept of method expressions, but with a bit more utility. Method values If the expression x has static type T and M is in the method set of type T, x.M is called a method value. The method value x.M is a function value that is callable with the same arguments as a method call of x.

Method expressions, conclusion

Today we’ll finish our discussoin of method expression from Monday and Tuesday. Method expressions … Function values derived from methods are called with function call syntax; the receiver is provided as the first argument to the call. That is, given f := T.Mv, f is invoked as f(t, 7) not t.f(7). We’ve already seen examples of this, the last two days. But to be honest, you’re unlikely to use these types of functions very frequently.