...

/

Strings

Strings

This lesson discusses the datatype string in detail.

Introduction

Strings are a sequence of UTF-8 characters (the 1-byte ASCII-code is used when possible, and a 2-4 byte UTF-8 code when necessary). UTF-8 is the most widely used encoding. It is the standard encoding for text files, XML files, and JSON strings. With string data type, you can reserve 4 bytes for characters, but Go is intelligent enough that it will reserve one-byte if the string is only an ASCII character.

Strings in Go

Contrary to strings in other languages as C++, Java or Python that are fixed-width (Java always uses 2 bytes), a Go string is a sequence of variable-width characters (each 1 to 4 bytes).

Advantages of strings

The advantages of strings are:

  • Go strings and text files occupy less memory/disk space (because of variable-width
...