Beginner Fundamentals

Strings

A string in Go is an immutable sequence of bytes, usually holding UTF-8 text. Immutable means you cannot change a string in place; you create a new one instead.

Creating and joining

first := "Hello"
second := "World"
joined := first + ", " + second + "!"
fmt.Println(joined) // Hello, World!

Length and indexing

len returns the number of bytes, and indexing returns a byte.

s := "Go"
fmt.Println(len(s))  // 2
fmt.Println(s[0])    // 71 (byte value of 'G')

The strings package

The standard library has many helpers.

import "strings"

fmt.Println(strings.ToUpper("go"))          // GO
fmt.Println(strings.Contains("golang", "go")) // true
fmt.Println(strings.Replace("a-b-c", "-", "+", -1)) // a+b+c

Runes

A rune represents a Unicode code point. Ranging over a string yields runes, which handles multi-byte characters correctly.

for i, r := range "héllo" {
    fmt.Printf("%d: %c\n", i, r)
}

Use runes when you need to count or process characters rather than raw bytes.