> For the complete documentation index, see [llms.txt](https://note.levelcode.org/software-engineering-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://note.levelcode.org/software-engineering-notes/language-and-framework-specific-notes/go-golang.md).

# Go (Golang)

## Getting Started

{% embed url="<https://go.dev/tour>" %}

{% embed url="<https://gobyexample.com/>" %}

## Important Stuff to Note

* Go does not have classes.
* In general, all methods on a given type should have either value or pointer receivers, but not a mixture of both.
* A type implements an interface by implementing its methods. There is no explicit declaration of intent, no "implements" keyword. <https://go.dev/tour/methods/10>
* Goroutine is more or less similar to Java's process fork. The goroutine will also be stopped if the main process is stopped.
  * Communications can be done between goroutines with channel.

#### Method with Pointer Receiver

A method with pointer receiver behaves differently:

```go
func (v *Vertex) Scale(f float64) {
    ...
}
```

The vertex's value will change on the struct. This is **similar** to having `func Scale(v *Vertex, f float64)` which will use the same Vertex instead of copying the Vertex.

## Closure

It might not be a good idea to use closures a lot, this is because closures do have a side-effect.

It is still okay to use it sparingly, one of the use-case where I might use it is for memoization:

{% embed url="<https://stackoverflow.com/a/67788470>" %}

## Struct Embedding

{% embed url="<https://gobyexample.com/struct-embedding>" %}

If a struct's field do not have a name, it is a struct embedding.


---

# 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://note.levelcode.org/software-engineering-notes/language-and-framework-specific-notes/go-golang.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.
