Strings

Contents
String literals
Concatenate and compare
Strings are bytes
strings.Builder

String literals

Double quoted strings interpret escape sequences such as newline markers, while raw strings in backticks keep every character as written.

Raw strings are handy for file paths, patterns, and multiline templates where backslashes must stay untouched.

package main import "fmt" func main() { a := "line one\nline two" b := `C:\tools\go\bin` fmt.Println(a) fmt.Println(b) }

Concatenate and compare

The plus operator joins strings, and the double equals operator compares them byte by byte.

Comparison is case sensitive, so normalize the case first when user input may vary.

package main import "fmt" func main() { hello := "Hello, " + "Gopher" fmt.Println(hello) fmt.Println(hello == "Hello, Gopher") fmt.Println(hello == "hello, gopher") }

go run .

Hello, Gopher
true
false

Strings are bytes

A Go string is a read only sequence of bytes, so len reports the byte count and indexing returns a single byte value.

For text processing, prefer a range loop: it decodes one rune per step and also reports the byte position.

package main import "fmt" func main() { s := "Go!" fmt.Println(len(s)) for i := 0; i < len(s); i++ { fmt.Println(i, s[i]) } }

go run .

3
0 71
1 111
2 33

strings.Builder

Joining many pieces with plus in a loop is slow because every step copies the whole result, so the strings package offers Builder for efficient assembly.

Write each fragment with WriteString and collect the final text once with String.

package main import ( "fmt" "strings" ) func main() { var b strings.Builder b.WriteString("Go") b.WriteString(" is ") b.WriteString("fast") fmt.Println(b.String()) }

go run .

Go is fast

Next: Conditions with if

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: