HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GoVariablesData TypesOperatorsStringsArray & SliceMap (Dictionary)Conditions (if / switch)Loops (for)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & Slice
🗂️ Map
🔁 Condition & Loop
🧩 Function
Go›Strings

Strings

Length with len, case conversion, splitting, and joining with the strings package.

Simple enough for a 5-year-old to understand

Length and uppercase

Example code
1
package main
2
import (
Multiple packages.
ImportMultiple packages.
3
"fmt"
4
"strings"
Text manipulation package.
ImportText manipulation package.
5
)
6
func main() {
7
fmt.Println(len("hello"))
7
Output / Input7
8
fmt.Println(strings.ToUpper("hello"))
HELLO
Output / InputHELLO
9
}
ImportOutput / Input
💡 Hint: Most text functions are in the strings package; don't forget to import it.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the word "hexlibrary" in UPPERCASE using strings.ToUpper.

🎯 Expected output: HEXLIBRARY

Loading editor…

About this topic

In Go, a string is an IMMUTABLE byte slice and is UTF-8 encoded. These three properties together determine the entire behavior of Go string manipulation: you cannot modify a string, indexing gives you bytes, and multi-byte characters require caution.

When you read strings with indexing, you get BYTES, not characters. To iterate character by character, range is used — when range traverses a string, Go automatically decodes UTF-8 and gives you code points. This is the correct way when working with Turkish content.

Concatenating strings inside a loop generates a new string on every iteration. In Go, strings.Builder is used for this job, and the difference is noticeable with thousands of concatenations.

What you will learn

  • that strings are byte slices
  • counting bytes with len and characters with RuneCountInString
  • iterating over characters with range
  • the basic functions of the strings package
  • using Split and Join
  • efficient concatenation with strings.Builder

When to use?

The strings package covers all of string processing: searching, replacing, trimming, splitting, uppercase and lowercase. Checking this package before writing your own string function is the right reflex.

Raw string literals with backticks do not interpret escape sequences and can be multi-line. They are very well suited for regular expressions, file paths, and SQL queries.

Common mistakes

Wrong

s := "Tree"
fmt.Println(len(s))
fmt.Println(string(s[1]))

len counts bytes and returns 6; s[1] takes HALF of a multi-byte character and produces corrupted output. To work with characters, you need to convert to a rune slice or use range.

Correct

s := "Tree"
r := []rune(s)
fmt.Println(len(r))
fmt.Println(string(r[1]))

Wrong

result := ""
for i := 0; i < 10000; i++ {
	result += strconv.Itoa(i)
}

Because strings are immutable, every iteration produces a new string; ten thousand iterations mean ten thousand allocations and the process slows down noticeably. strings.Builder grows the same buffer.

Correct

var sb strings.Builder
for i := 0; i < 10000; i++ {
	sb.WriteString(strconv.Itoa(i))
}
result := sb.String()

Wrong

s := "hello"
s[0] = 'M'

Strings are immutable; assignment by index gives the "cannot assign to s[0]" error. To modify them, you need to convert to a byte or rune slice and back.

Correct

s := "hello"
b := []rune(s)
b[0] = 'M'
s = string(b)

Frequently asked questions

Why doesn't len return the character count?

Because Go strings are UTF-8 encoded byte slices and len counts bytes. Since Turkish characters take up two bytes, the result comes out larger than you expect. utf8.RuneCountInString is used for the character count.

What is the difference between rune and byte?

byte holds a single byte, while rune holds a single Unicode code point. In ASCII text, they yield the same result; in text containing Turkish, Arabic, or emojis, they differ. If you are working with characters, use rune; if you are working with binary data, use byte.

How can I concatenate strings efficiently?

If inside a loop, with strings.Builder. If you are combining a few pieces at once, strings.Join or fmt.Sprintf is sufficient. The difference only becomes measurable when the number of concatenations exceeds the hundreds.

Summary

Go strings are immutable, UTF-8 encoded byte slices: len counts bytes, indexing gives bytes, and range or a rune slice is used to iterate over characters. strings.Builder is preferred for concatenation inside loops.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code