go:love_of_go
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| go:love_of_go [2025/10/15 14:09] – [MAPS] v1ctor | go:love_of_go [2025/11/18 10:39] (current) – [ERRORS] v1ctor | ||
|---|---|---|---|
| Line 67: | Line 67: | ||
| Running a test for a package: | Running a test for a package: | ||
| - | < | + | < |
| $ go test | $ go test | ||
| + | $ go test -count 100 # repeat a test 100 times | ||
| </ | </ | ||
| Line 82: | Line 83: | ||
| </ | </ | ||
| - | ===== ERRORS | + | ==== ERRORS ==== |
| - | Creating | + | Go has a type to communicate errors - **error**. Example usage: |
| <code go> | <code go> | ||
| - | return | + | func (book *Book) SetCopies(copies int) error { |
| + | if copies < 0 { | ||
| + | return fmt.Errorf("negative number of copies: %d", copies) | ||
| + | } | ||
| + | book.Copies = copies | ||
| + | return nil | ||
| + | } | ||
| </ | </ | ||
| - | ===== VARIABLES | + | <code go> |
| + | err := book.SetCopies(-1) | ||
| + | if err != nil { | ||
| + | fmt.Println(" | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== VARIABLES | ||
| Go assigns a default **zero** value to any variable, that is declared but not assigned a value. | Go assigns a default **zero** value to any variable, that is declared but not assigned a value. | ||
| Line 107: | Line 121: | ||
| - | ===== SLICES | + | ==== SLICES ==== |
| Slice can be declared as a **nil** or as an **empty** slice: | Slice can be declared as a **nil** or as an **empty** slice: | ||
| Line 124: | Line 138: | ||
| </ | </ | ||
| - | ===== MAPS ===== | + | **Sorting a Slice of Struct** |
| + | <code go> | ||
| + | |||
| + | type Person struct { | ||
| + | Name string, | ||
| + | Age int, | ||
| + | } | ||
| + | |||
| + | people := []Person{ | ||
| + | {" | ||
| + | {" | ||
| + | } | ||
| + | |||
| + | slices.SortFunc(people, | ||
| + | return cmp.Compare(a.Age, | ||
| + | }) // cmp.Compare - a helper that returns -1, 0, or 1 | ||
| + | </ | ||
| + | |||
| + | ==== MAPS ==== | ||
| Map is a reference type - when we pass it to the function, we are passing a reference, not a copy. | Map is a reference type - when we pass it to the function, we are passing a reference, not a copy. | ||
| Line 181: | Line 213: | ||
| book, ok := catalog[" | book, ok := catalog[" | ||
| </ | </ | ||
| - | ===== STRUCTS | + | |
| + | ==== STRUCTS ==== | ||
| Empty literal: | Empty literal: | ||
| Line 203: | Line 236: | ||
| Instead, we can create a **struct** type, with a field of type we want. | Instead, we can create a **struct** type, with a field of type we want. | ||
| + | |||
| ==== COMMA, OK PATTERN ==== | ==== COMMA, OK PATTERN ==== | ||
| Line 217: | Line 251: | ||
| return Book{}, false | return Book{}, false | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | ==== POINTERS ==== | ||
| + | |||
| + | <code go> | ||
| + | x := 5 | ||
| + | y := & | ||
| + | |||
| + | fmt.Println(*y) | ||
| + | </ | ||
| + | |||
| + | ==== OBJECTS ==== | ||
| + | |||
| + | In order to create a Method, we need to specify the **receiver** parameter. It tells Go that this method should be called on a specific object value, and that value is available inside the method: | ||
| + | <code go> | ||
| + | func (book Book) String() string { // (book Book) - method' | ||
| + | return fmt.Sprintf(" | ||
| + | book.Title, book.Author, | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | There are two types of receivers - **Value** and **Pointer** receivers. Think of it as //how the function gets access to the object:// | ||
| + | * By Copy (value receiver) - this **copies the data** into the method | ||
| + | * By Value (pointer receiver) - this gives the method a **reference to the original object** | ||
| + | |||
| + | |||
| + | <code go> | ||
| + | func (book Book) SetCopies(copies int) { | ||
| + | book.Copies = copies | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | And the //pointer// example: | ||
| + | <code go> | ||
| + | func (book *Book) SetCopies(copies int) { | ||
| + | book.Copies = copies | ||
| + | // pointers to struct. Pointers can't have fields, so there | ||
| + | // is no ambiguity here. Otherwise it would look like: | ||
| + | // (*book).Copies = copies | ||
| + | } | ||
| + | |||
| + | book := books.Book{ | ||
| + | Copies: 5, | ||
| + | } | ||
| + | |||
| + | book.SetCopies(12) | ||
| + | // the method | ||
| + | |||
| </ | </ | ||
go/love_of_go.1760537349.txt.gz · Last modified: by v1ctor
