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/11/18 09:46] – [OBJECTS] v1ctorgo:love_of_go [2025/11/18 10:39] (current) – [ERRORS] v1ctor
Line 85: Line 85:
 ==== 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>
  
Line 270: Line 283:
 </code> </code>
  
-The the //pointer// example:+And the //pointer// example:
 <code go> <code go>
 func (book *Book) SetCopies(copies int) { func (book *Book) SetCopies(copies int) {
Line 278: Line 291:
                                     // (*book).Copies = copies                                     // (*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.1763459175.txt.gz · Last modified: by v1ctor