Skip to main content

Command Palette

Search for a command to run...

Strings in Go

Updated
3 min read
Strings in Go
P

I am a self-taught Fullstack Web and Mobile App developer with over 4 years of experience. I have built a few apps using React and React Native with a backend in Nest.JS and Express.JS

In this lesson, we'll dive deep into the string datatype in Go. Strings are a sequence of UTF-8 characters. Go efficiently handles string memory by reserving only 1 byte for ASCII characters when possible, and between 2-4 bytes for UTF-8 characters when necessary. UTF-8 is the most common text encoding, and it is used widely across formats like text files, XML, and JSON.


Strings in Go

Unlike other languages like C++, Java, or Python that use fixed-width characters (e.g., Java always uses 2 bytes for characters), Go strings are made up of variable-width characters. Each character in a Go string can be between 1 and 4 bytes long.

Advantages of Strings in Go

  • Memory Efficiency: Go strings take up less memory and disk space due to the use of variable-width characters.

  • UTF-8 Standard: Go uses UTF-8 as the default encoding, avoiding the need to convert between different string encodings.

  • Immutability: Strings in Go are immutable, meaning once created, their contents cannot be changed. They are essentially immutable arrays of bytes.


Types of String Literals in Go

Go supports two types of string literals:

  1. Interpreted Strings: These are enclosed in double quotes ("") and interpret escape sequences like \n, \r, \t, and Unicode characters (\u, \U). For example:

     "\n"  // Newline
     "\t"  // Tab
    
  2. Raw Strings: Enclosed in backticks (``````), raw strings do not interpret any escape sequences. For example:

     `This is a raw string \n`
    

    In this case, \n is treated literally and not as a newline. Raw strings can also span multiple lines.


Length of a String in Go

Unlike C/C++, Go strings do not end with a special terminating character. The default value of a string is an empty string "". Go allows standard comparison operators (==, !=, <, <=, >=, >) for string comparisons, which are done byte by byte.

You can find the length (in bytes) of a string using the len() function:

len(str)

String indexing starts at 0, meaning the first character can be accessed with str[0]. For example, with the string "Hello", you can access characters like this:

H = str[0]
e = str[1]

The last character can be accessed with str[len(str)-1]. However, note that Go doesn't allow taking the address of a character in a string.


String Concatenation in Go

Go allows the concatenation of two or more strings using the + operator. For example:

s1 := "Hello"
s2 := "World"
s := s1 + s2  // Result: "HelloWorld"

To create multi-line strings, you can split them across lines, but ensure that the + operator stays at the end of the first line:

str := "This is the first part " +
"and this is the second part"

Alternatively, you can use the shorthand += to append strings:

s1 += s2  // s1 becomes "HelloWorld"

This covers the basics of strings in Go, including their structure, types, and operations.


In the next lesson, you'll practice by writing a function to solve a problem using Go strings.

35 views

Learn GO

Part 4 of 13

In this series, I will write articles that help you understand go! I am learning go so I am writing this series as a note for myself and hope you also love this series and learn along with me!

Up next

Operators in Go

In Go, operators are symbols used to perform operations, such as arithmetic, logical, or bitwise operations. This lesson will cover different types of operators and their behaviour, along with examples and ASCII diagrams where needed. Arithmetic Ope...