Slice expression result types

No live stream today. Next week I expect to be back at it! Forgive me for not sending an email Friday. I spent the day in bed. 🛏️ But I’m back on my feet now! So up to now we’ve learned how to use slice expressions… but what is the type that results? Well, you can probably guess. It’s pretty intuitive: Simple slice expressions … Except for untyped strings, if the sliced operand is a string or slice, the result of the slice operation is a non-constant value of the same type as the operand.


Range of slice expressions

Simple slice expressions … For arrays or strings, the indices are in range if 0 <= low <= high <= len(a), otherwise they are out of range. This is probably intuitive. a := [5]int{1, 2, 3, 4, 5} s := "Abracadabra" a[-1] // Out of range s[5:1] // Out of range a[12] // Out of range s[999] // Out of range For slices, the upper index bound is the slice capacity cap(a) rather than the length.

Quiz Challenge

1 min read


Quiz

Today’s quiz day. Tomorrow I’ll provide the answer as we continue with the section of the spec that explains today’s code. Consider the following program. What is its output? package main import "fmt" func main() { a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} s := a[2:5] s2 := s[5:7] fmt.Println(s2) } [6 7] [8 9] A runtime panic A compilation error


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.


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.


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.


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 <.

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.


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.