Last active
February 6, 2019 16:02
-
-
Save lbragstad/d006073b9dd739bc29a30b2d8e8bfc83 to your computer and use it in GitHub Desktop.
The difference in returning values and errors from methods in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
) | |
// Triangle is a struct for modeling triangle shapes | |
type Triangle struct { | |
base, height float64 | |
} | |
// Square is a struct for modeling a square shapes | |
type Square struct { | |
sideLength float64 | |
} | |
type shape interface { | |
getArea() float64 | |
} | |
func (t Triangle) getArea() float64 { | |
return t.base * t.height / 2 | |
} | |
func (s Square) getArea() float64 { | |
return s.sideLength * s.sideLength | |
} | |
func printArea(s shape) { | |
fmt.Println("Area:", s.getArea()) | |
} | |
// NewSquare returns a pointer to a new Square struct. | |
func NewSquare(sl float64) (*Square, error) { | |
if sl < 0 { | |
return nil, errors.New("square cannot have sides with negative values") | |
} | |
p := &Square{sideLength: sl} | |
return p, nil | |
} | |
// NewTriangle returns a pointer to a new Triangle struct. | |
func NewTriangle(b float64, h float64) (*Triangle, error) { | |
if b < 0 || h < 0 { | |
return nil, errors.New("triangles cannot have negative values") | |
} | |
p := &Triangle{base: b, height: h} | |
return p, nil | |
} | |
func main() { | |
spointer, err := NewSquare(5) | |
if err != nil { | |
fmt.Errorf("unable to create square: %v", err) | |
} | |
s := *spointer | |
tpointer, err := NewTriangle(5, 8) | |
if err != nil { | |
fmt.Errorf("unable to create triangle: %v", err) | |
} | |
t := *tpointer | |
printArea(s) | |
printArea(t) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
) | |
// Triangle is a struct for modeling triangle shapes | |
type Triangle struct { | |
base, height float64 | |
} | |
// Square is a struct for modeling a square shapes | |
type Square struct { | |
sideLength float64 | |
} | |
type shape interface { | |
getArea() float64 | |
} | |
func (t Triangle) getArea() float64 { | |
return t.base * t.height / 2 | |
} | |
func (s Square) getArea() float64 { | |
return s.sideLength * s.sideLength | |
} | |
func printArea(s shape) { | |
fmt.Println("Area:", s.getArea()) | |
} | |
// NewSquare returns a a new Square struct. | |
func NewSquare(sl float64) (Square, error) { | |
if sl < 0 { | |
s := Square{sideLength: 0} | |
return s, errors.New("square cannot have sides with negative values") | |
} | |
return Square{sideLength: sl}, nil | |
} | |
// NewTriangle returns a new Triangle struct. | |
func NewTriangle(b float64, h float64) (Triangle, error) { | |
if b < 0 || h < 0 { | |
t := Triangle{base: 0, height: 0} | |
return t, errors.New("triangles cannot have negative values") | |
} | |
return Triangle{base: b, height: h}, nil | |
} | |
func main() { | |
s, err := NewSquare(5) | |
if err != nil { | |
fmt.Errorf("unable to create square: %v", err) | |
} | |
t, err := NewTriangle(5, 8) | |
if err != nil { | |
fmt.Errorf("unable to create triangle: %v", err) | |
} | |
printArea(s) | |
printArea(t) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment