When a string's length is constant

February 27, 2023

String Types

The length of a string s can be discovered using the built-in function len. The length is a compile-time constant if the string is a constant. A string’s bytes can be accessed by integer indices 0 through len(s)-1. It is illegal to take the address of such an element; if s[i] is the i‘th byte of a string, &s[i] is invalid.

There are a couple of nuggets in this paragraph.

First, you may recall from our introduction to constants that a constant may represent “… the result value of some built-in functions such as … len applied to some expressions.

Now we have an example of such an expression: Calling len on a string constant returns a constant value. So this is valid:

const str = "Hello, World"
const strLen = len(str) // 12

However, this is not:

var str = "Hello, " + name
const strLen = len(str) // Invalid (unless `name` itself is a string constant as well)

Second, the final sentence is interesting for reasons that aren’t immediately obvious.

First note that taking the address of a slice is perfectly valid:

	var s = []byte("Hello, World")
	var x = &s[3]

	fmt.Println(x) // 0xc00001c033 or some other memory location

Now that I have a pointer to a specific byte in my slice, I can do things with it, including modifying it:

	var s = []byte("Hello, World")
	var x = &s[3]
	*x = 'X'

	fmt.Println(string(s)) // HelXo, World

But if we could do the same thing with a string, we would violate the immutability trait of strings.

This is why it is impossible to take the address of an element of a string.

Quotes from The Go Programming Language Specification, Version of January 19, 2023

Share this

Related Content

Empty structs

We finally we have enough knowledge for the EBNF format not to seem completely foreign, so let’s jump back and take a look at that, with the examples provided in the spec… Struct types … StructType = "struct" "{" { FieldDecl ";" } "}" . FieldDecl = (IdentifierList Type | EmbeddedField) [ Tag ] . EmbeddedField = [ "*" ] TypeName [ TypeArgs ] . Tag = string_lit . // An empty struct.

Struct tags

Struct types … A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. An empty tag string is equivalent to an absent tag. The tags are made visible through a reflection interface and take part in type identity for structs but are otherwise ignored. struct { x, y float64 "" // an empty tag string is like an absent tag name string "any string is permitted as a tag" _ [4]byte "ceci n'est pas un champ de structure" } // A struct corresponding to a TimeStamp protocol buffer.

Struct method promotion

Yesterday we saw an example of struct field promotion. But methods (which we haven’t really discussed yet) can also be promoted. Struct types … Given a struct type S and a named type T, promoted methods are included in the method set of the struct as follows: If S contains an embedded field T, the method sets of S and *S both include promoted methods with receiver T. The method set of *S also includes promoted methods with receiver *T.