1 min read
Simple slice expression shortcuts
Simple slice expressions, which we started with yesterday, offer us two shortcuts: Simple slice expressions … For convenience, any of the indices may be omitted. A missing low index defaults to zero; a missing high index defaults to the length of the sliced operand: a[2:] // same as a[2 : len(a)] a[:3] // same as a[0 : 3] a[:] // same as a[0 : len(a)] This should be pretty easy to understand.
2 min read
Slice expressions
Last week’s livestream turned out to be a bit of a disappointment, as some Docker problems prevented me from making any real progress. I’ve resolved that, so today I hope to hit the ground running with some Kubernetes deployments! I hope you can join me again today on my live stream. Ever wonder why Go uses the terminology “slice”, rather than “array”, to refer to a variable list of things? Today’s section explains that.
2 min read
Index expression special cases for maps
Today we’re finishing up our discussion of… Index expressions … So far, we’ve discussed what index expressions are, and specific rules as they apply to: arrays slices and strings maps type parameters Otherwise a[x] is illegal. But there are two more special cases to consider for maps: An index expression on a map a of type map[K]V used in an assignment statement or initialization of the special form v, ok = a[x] v, ok := a[x] var v, ok = a[x] yields an additional untyped boolean value.
2 min read
Index expressions for type parameters
Index expressions … For a of type parameter type P: So just a reminder what this means: We’re talking about index expressions for different data types. And we’re going through different interpretations of the expression a[x]. In this example, a is of a “type parameter” type, represented by P. And as a further reminder, a type parameter looks something like [X any] or [Y ~int]. So for this example, we’re assuming that we have [P <.
2 min read
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.
2 min read
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.
2 min read
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.
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.
2 min read
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.
2 min read
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.
2 min read
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.
2 min read
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.