Skip to Content
marmot
DiagnosticsError codes
example $ marmot build .
error[M0014]: cannot use Square{…} (value of struct type Square) as Shape value in
              variable declaration: Square does not implement Shape (method Area has
              pointer receiver)
 --> ./main.go:7:15
  |
3 | type Shape interface{ Area() float64 }
  |      ----- `Shape` is declared here
...
5 | func (q *Square) Area() float64 { return q.s * q.s }
  |         ------- `Area` has a pointer receiver
...
7 | var s Shape = Square{2}
  |               ^^^^^^^^^ `Square` does not implement `Shape`
  |
  = note: `Square` lacks 1 method of `Shape`:
            `Area` has a pointer receiver, so only `*Square` has it
help: pass a pointer (check it)
  |
7 - var s Shape = Square{2}
7 + var s Shape = &Square{2}
  |

error: build failed with 1 error
for more about an error, run `marmot explain M0014`
The program that produced it
package main

type Shape interface{ Area() float64 }
type Square struct{ s float64 }
func (q *Square) Area() float64 { return q.s * q.s }

var s Shape = Square{2}

func main() {}

The mistake and the fix

The mistake
type Shape interface{ Area() float64 }
type Square struct{ s float64 }
func (q *Square) Area() float64 { return q.s * q.s }

var s Shape = Square{2}
Fixed
var s Shape = &Square{2} // *Square has Area

The fix

Add the missing method, fix its signature, or pass a pointer when the method has a pointer receiver.

See also all error codes and marmot explain.

On this page

Was this page helpful?

Report an issueOpen issues