Create a gist now

Instantly share code, notes, and snippets.

@Xe /foo.go Secret
Last active Aug 29, 2015

What would you like to do?
/*
Package foo supports a quux operation for flapnars.
*/
package foo
import (
"encoding/json"
"errors"
"fmt"
"time"
)
// An error for an invalid owner
var (
ErrWrongOwner = errors.New("You may not use that person as an owner")
)
// Flapnars hold a trinket and its metadata for cloud synergy.
type Flapnar struct {
Trinket string `json:"trinket"` // This is how you declare json -> struct mapping
Owner string `json:"owner"` // This is commonly called "struct tagging"
time time.Time `json:"-"` // don't encode this into json, also make this private
}
// New makes a new Flapnar with a given trinket and owner.
func New(trinket, owner string) (f Flapnar, err error) {
if owner == "Xena" {
return f, ErrWrongOwner
}
f = Flapnar{
Trinket: trinket,
Owner: owner,
time: time.Now(),
}
return // If you declare named returns you can have an empty return
}
// Quux does an operation with a Flapnar.
func (f Flapnar) Quux() {
fmt.Println(f.String())
}
// String returns a string representation of the Flapnar to satisfy fmt.Stringer.
func (f Flapnar) String() string {
return fmt.Sprintf("<Flapnar: Trinket: %s, Owner: %s>", f.Trinket, f.Owner)
}
// Marshal returns a json encoded version of a Flapnar and an error or nil if the
// json encoding failed.
func (f Flapnar) Marshal() (res []byte, err error) {
res, err = json.Marshal(&f) // use a pointer to the flapnar because of how reflection works
return
}
package foo
import (
"testing"
)
func TestMakeFlapnar(t *testing.T) {
_, err := New("Goron Shield", "Link")
// _ lets you shadow returns from a function to explicitly ignore them.
if err != nil {
t.Fatal(err)
}
}
func TestDontMakeFlapnar(t *testing.T) {
_, err := New("Surface Pro 3", "Xena")
if err != ErrWrongOwner {
t.Fatalf("Expected ErrWrongOwner, got %#v", err)
}
}
func TestFlapnarString(t *testing.T) {
flap, err := New("Amazing Potion", "Dusky")
if err != nil {
t.Fatal(err)
}
expected := `<Flapnar: Trinket: Amazing Potion, Owner: Dusky>`
if flap.String() != expected {
t.Fatalf("Expected %s, got %s", expected, flap.String())
}
}
func TestFlapnarQuux(t *testing.T) {
flap, err := New("Book of Haskell", "Sephira")
if err != nil {
t.Fatal(err)
}
flap.Quux()
}
func TestFlapnarMarshal(t *testing.T) {
flap, err := New("Book of Rants", "Cassy")
if err != nil {
t.Fatal(err)
}
_, err = flap.Marshal()
if err != nil {
t.Fatal(err)
}
}
use 'godoc cmd//target' for documentation on the /target command
PACKAGE DOCUMENTATION
package foo
import "."
Package foo supports a quux operation for flapnars.
VARIABLES
var (
ErrWrongOwner = errors.New("You may not use that person as an owner")
)
An error for an invalid owner
TYPES
type Flapnar struct {
Trinket string `json:"trinket"` // This is how you declare json -> struct mapping
Owner string `json:"owner"` // This is commonly called "struct tagging"
// contains filtered or unexported fields
}
Flapnars hold a trinket and its metadata for cloud synergy.
func New(trinket, owner string) (f Flapnar, err error)
New makes a new Flapnar with a given trinket and owner.
func (f Flapnar) Marshal() (res []byte, err error)
Marshal returns a json encoded version of a Flapnar and an error or nil
if the json encoding failed.
func (f Flapnar) Quux()
Quux does an operation with a Flapnar.
func (f Flapnar) String() string
String returns a string representation of the Flapnar to satisfy
fmt.Stringer.
$ go test -v
=== RUN TestMakeFlapnar
--- PASS: TestMakeFlapnar (0.00 seconds)
=== RUN TestDontMakeFlapnar
--- PASS: TestDontMakeFlapnar (0.00 seconds)
=== RUN TestFlapnarString
--- PASS: TestFlapnarString (0.00 seconds)
=== RUN TestFlapnarQuux
<Flapnar: Trinket: Book of Haskell, Owner: Sephira>
--- PASS: TestFlapnarQuux (0.00 seconds)
=== RUN TestFlapnarMarshal
--- PASS: TestFlapnarMarshal (0.00 seconds)
PASS
ok _/home/xena/go/src/foo 0.001s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment