# Arrays and Slices

> Let's learn about Go's arrays and slices

\ <br>

### Arrays

* In Go, when creating an array, you must specify the **length** of that array
  * ex)

    ```go
    package main

    import "fmt"

    func main() {
     names := [5]string{"chloe", "camila", "bella"}
     names[3] = "charlotte"
     names[4] = "bree"
     fmt.Print(names)
    }
    ```

    * Go's **index** starts from **0**, so in the above example, names\[4] is the 5th index
  * If you put an index larger than the **specified length** when creating an array, an **error** will occur

\ <br>

### Slices

* Sometimes you want to limit the size of an array, but sometimes you want to add elements **without size limitation**
  * The data type you can use in such cases is **slice**
* Slice in Go is an array but **without length**
  * ex)

    ```go
    package main

    import "fmt"

    func main() {
     states := []string{"Florida", "Kentucky"}
     fmt.Println(states)
    }
    ```

    * The only difference from array is that there's no **length**

<br>

#### append()

* When **adding** **items** to a slice, use the `append()` function
* append() takes two arguments:
  1. **slice**
  2. **value**
* However, `append()` does not modify the **slice**
  * `append()` **returns** a slice with new values added!
    * So you need to update the slice again
    * ex)

      ```go
      package main

      import "fmt"

      func main() {
       states := []string{"Florida", "Kentucky"}
       states = append(states, "California")
       fmt.Println(states)
      }
      ```

<br>

*I think I'll probably use slice most of the time!*


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chloe-codes1.gitbook.io/til/go/go-101/08_arrays_and_slices.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
