# Strings in Go

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:
    
    ```go
    "\n"  // Newline
    "\t"  // Tab
    ```
    
2. **Raw Strings**: Enclosed in backticks (\`\`\`\`\`\`), raw strings do not interpret any escape sequences. For example:
    
    ```go
    `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:

```go
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:

```go
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:

```go
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:

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

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

```go
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.
