User Tools

Site Tools


go:love_of_go

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
go:love_of_go [2025/10/16 09:17] – [SLICES] v1ctorgo:love_of_go [2025/11/18 10:39] (current) – [ERRORS] v1ctor
Line 83: Line 83:
 </code> </code>
  
-===== ERRORS =====+==== ERRORS ====
  
-Creating custom error:+Go has type to communicate errors - **error**. Example usage:
 <code go> <code go>
-return 0, errors.New("division by zero not allowed")+func (book *Book) SetCopies(copies int) error { 
 +  if copies < 
 +    return fmt.Errorf("negative number of copies: %d"copies) 
 +  } 
 +  book.Copies = copies 
 +  return nil 
 +
 +</code> 
 + 
 +<code go> 
 +err := book.SetCopies(-1) 
 +if err != nil { 
 +  fmt.Println("Oh dear, something went wrong:", err) 
 +}
 </code> </code>
  
-===== VARIABLES =====+==== 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 108: 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 127: Line 140:
 **Sorting a Slice of Struct** **Sorting a Slice of Struct**
 <code go> <code go>
-slices.SortFunc(people, func(a,b Person) int {   // slice.SortFunc - generic function for sorting slices with + 
-  return cmp.Compare(a.Age, b.Age)               // a custom comparison.+type Person struct { 
 +  Name string, 
 +  Age int, 
 +
 + 
 +people := []Person{ 
 +  {"Alice", 25}, 
 +  {"Bob", 30}, 
 +
 + 
 +slices.SortFunc(people, func(a,b Person) int {   // slice.SortFunc - generic function for sorting slices 
 +  return cmp.Compare(a.Age, b.Age)               // with a custom comparison.
 })                                               // cmp.Compare - a helper that returns -1, 0, or 1 })                                               // cmp.Compare - a helper that returns -1, 0, or 1
 </code> </code>
-===== MAPS =====+ 
 +==== 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 188: Line 213:
 book, ok := catalog["some-key]"  // maps support "comma, ok" pattern. ok == false if key is missing book, ok := catalog["some-key]"  // maps support "comma, ok" pattern. ok == false if key is missing
 </code>  </code> 
-===== STRUCTS =====+ 
 +==== STRUCTS ====
  
 Empty literal: Empty literal:
Line 210: 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 224: Line 251:
   return Book{}, false   return Book{}, false
 } }
 +</code>
 +
 +==== POINTERS ====
 +
 +<code go>
 +x := 5
 +y := &          // `y` is a pointer to `x`. `&` is an address operator
 + 
 +fmt.Println(*y)   // *y dereferences y - it retrieves the value that y “points” to
 +</code> 
 +
 +==== 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's receiver
 +  return fmt.Sprintf("%v by %v (copies: %v)",
 +    book.Title, book.Author, book.Copies)
 +}
 +</code>
 +
 +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                    // only affects local `book`, not the original
 +}
 +</code>
 +
 +And the //pointer// example:
 +<code go>
 +func (book *Book) SetCopies(copies int) {
 + book.Copies = copies               // Go provides an automatic de-referencing when we use
 +                                           // 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)                         // That's here, when we pass `value` or `reference` to
 +                                           // the method
 +
 </code> </code>
go/love_of_go.1760606259.txt.gz · Last modified: by v1ctor