car.go
package car
import "errors"
// Car struct
type Car struct {
Name string
Price float32
}
// SetName set car name
func (c *Car) SetName(name string) string {
if name != "" {
c.Name = name
}
return c.Name
}
// New Object
func New(name string, price float32) (*Car, error) {
if name == "" {
return nil, errors.New("missing name")
}
return &Car{
Name: name,
Price: price,
}, nil
}
car_test.go
package car
import (
"testing"
)
// Simple testing what different between Fatal and Error
func TestNew(t *testing.T) {
c, err := New("", 100)
if err != nil {
t.Error("got errors:", err)
}
if c == nil {
t.Error("car should be nil")
}
}
下command
go test -v