</>
Skip to content
Go lessons (10/34)

Go — Strings

String basics

package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "Hello, World!"
    raw := `Raw string
with newlines`

    fmt.Println(s)
    fmt.Println(raw)
    fmt.Println(len(s))         // 13
    fmt.Println(s[0])           // 72 (byte value)
    fmt.Println(string(s[0]))   // H
}

String operations

s := "Hello, World!"

fmt.Println(len(s))                          // 13
fmt.Println(strings.ToUpper(s))              // HELLO, WORLD!
fmt.Println(strings.ToLower(s))              // hello, world!
fmt.Println(strings.Contains(s, "World"))    // true
fmt.Println(strings.HasPrefix(s, "Hello"))   // true
fmt.Println(strings.HasSuffix(s, "!"))       // true
fmt.Println(strings.Index(s, "World"))       // 7
fmt.Println(strings.Replace(s, "World", "Go", 1)) // Hello, Go!
fmt.Println(strings.Repeat("ab", 3))         // ababab

Split and join

csv := "apple,banana,cherry"

// Split
fruits := strings.Split(csv, ",")
fmt.Println(fruits) // [apple banana cherry]

// Join
joined := strings.Join(fruits, " | ")
fmt.Println(joined) // apple | banana | cherry

String formatting

name := "Alice"
age := 30

// fmt.Sprintf
s := fmt.Sprintf("Name: %s, Age: %d", name, age)
fmt.Println(s)

// Format specifiers
fmt.Printf("%q\n", "hello")       // "hello"
fmt.Printf("%-10s\n", "left")     // "left      "
fmt.Printf("%10s\n", "right")     // "     right"
fmt.Printf("%05d\n", 42)          // 00042
fmt.Printf("%.2f\n", 3.14159)     // 3.14

Bytes and runes

s := "Hello, 你好!"

// Bytes
bytes := []byte(s)
fmt.Println(bytes)

// Runes (Unicode code points)
runes := []rune(s)
fmt.Println(runes)
fmt.Println(len(runes)) // 9 (not 13)

// Iterate by rune
for i, r := range s {
    fmt.Printf("Index %d: %c\n", i, r)
}

String conversion

import (
    "strconv"
    "fmt"
)

// Number to string
s := strconv.Itoa(42)     // "42"
s = strconv.FormatFloat(3.14, 'f', 2, 64) // "3.14"

// String to number
n, err := strconv.Atoi("42")
f, err := strconv.ParseFloat("3.14", 64)

fmt.Println(s, n, f)

Builder

import "strings"

var builder strings.Builder
for i := 0; i < 1000; i++ {
    builder.WriteString("a")
}
result := builder.String()

Mini Practice

Write Go code that:

  1. Splits a CSV string into fields
  2. Joins a slice of strings with a delimiter
  3. Counts occurrences of a character in a string
  4. Uses strings.Builder for efficient string concatenation

Up Next

In the next lesson, you'll learn about If Else — conditional branching in Go.

Related Topics

Frequently Asked Questions about Strings

What is Strings in Go?

Strings is a fundamental concept in Go. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Strings?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Strings.

Why is Strings important in Go?

Strings is essential for Go development. Understanding this concept will help you write better code and solve real-world problems more effectively.