github.com/kaydxh/golang@v0.0.131/tutorial/programming_paradigm/interface.check_test.go (about) 1 package tutorial 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 type Shape interface { 9 Sides() int 10 Area() int 11 } 12 type Square struct { 13 length int 14 } 15 16 func NewSquare(length int) *Square { 17 return &Square{ 18 length: length, 19 } 20 } 21 22 func (s *Square) Sides() int { 23 return 4 24 } 25 26 func (s *Square) Area() int { 27 return 16 28 } 29 30 // check interface complete 31 // if not implete Area method, it will report InvalidIfaceAssign: cannot use (*Square)(nil) (value of type *Square) 32 //as Shape value in variable declaration: missing method Area 33 var _ Shape = (*Square)(nil) 34 35 func TestNewSquare(t *testing.T) { 36 testCases := []struct { 37 length int 38 expected string 39 }{ 40 { 41 length: 5, 42 expected: "", 43 }, 44 } 45 46 for i, testCase := range testCases { 47 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { 48 s := NewSquare(testCase.length) 49 t.Logf("s: %v", s.Sides()) 50 }) 51 } 52 }