# Structs

> Let's learn about the attractive struct

\ <br>

### Struct

* `Struct` is similar to **object** but is more flexible than **map**
* **map** can only contain `Key` and `Value` of specified types, but **struct** can contain values regardless of type
* `Struct` is basically a **structure**, and the way to create a struct is as follows:
  1. Define what kind of struct it is
  2. Create the structure format
  * ex)

    ```go
    package main

    import "fmt"

    type person struct {
     name    string
     age     int
     favFood []string
    }

    func main() {
     favFood := []string{"guacamole", "omelet", "biscuits and gravy"}
     chloe := person{"chloe", 26, favFood}
     fmt.Println(chloe)
    }
    ```

<br>

#### Specifying values in Struct

1. Specify values in the defined order

   ```go
   chloe := person{"chloe", 26, chloeFav}
   ```

   * This is not a good method because it's not clear which value is which
     * There's the inconvenience of having to check what position the value is in the `struct`
2. Specify by clearly stating what value it is

   ```go
   camila := person{name: "camila", age: 26, favFood: camilaFav}
   ```

* It's clear since you don't need to compare and check the `struct`

<br>

*However, you cannot use the two methods mixed together!*

<br>

#### Using Struct

* Go doesn't have **class** like Java, and doesn't have **object** like Python or JavaScript
  * You must use `Struct`!
* You can also create **methods** with Struct
* Go doesn't have **construct method**
  * There's no construct like Python's `__init__`, JavaScript's `constructor()` in Go's `struct`
  * You must create and execute the constructor yourself
* Since you can do almost everything in Go using `struct`, struct plays an important role!


---

# 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/10_structs.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.
