> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/golang-notes/official-tutorial-notes/more-types/structs.md).

# Structs

A `struct`  is a collection of fields. Struct fields are accessed using a dot.

### Example

```go
package main

import "fmt"

type Vertex struct {
    X int
    Y int
}

func main() {
    fmt.Println(Vertex{1, 2})
    
    v := Vertex{1, 2}
    v.X = 4
    fmt.Println(v.X)
}
```

```bash
{1 2}
4
```

## Pointers to Structs

Struct fields can also be accessed using a struct pointer.

```go
package main

import "fmt"

type Vertex struct {
    X int
    Y int
}

func main() {
    v := Vertex{1, 2}
    p := &v           // struct pointer p
    p.X = 1e9
    // (*p).X = 9     // output would be {9 2}
    fmt.Println(v)
}
```

```bash
{1000000000 2}
```

{% hint style="info" %}
We could write `(*p).X` to access the field `X` of the `Vertex` struct. However, this is a cumbersome notation. So, Go allows us to use **`p.X`** without this explicit derefence.
{% endhint %}

## Struct Literals

A struct literal denotes a newly allocated struct value by listing the values of its fields. The predix `&` returns a pointer to the struct value.

```go
package main

import "fmt"

type Vertex struct {
    X, Y int
}

var (
    v1 = Vertex{1, 2}
    v2 = Vertex{X: 1}    // using Name: syntax; Y: 0 is implicit
    v3 = Vertex{}        // X:0 and Y:0
    p = &Vertex{1, 2}    // has type *Vertex
)

func main() {
    fmt.Println(v1, v2, v3, p)
}

```

```bash
{1 2} {1 0} {0 0} &{1 2}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://blog.yushunchen.com/golang-notes/official-tutorial-notes/more-types/structs.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
