You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Document "Effective Go" under the Printing section shows how to write String()
function for object T:
func (t *T) String() string {
return fmt.Sprintf("%d/%g/%q", t.a, t.b, t.c)
}
fmt.Printf("%v\n", t)
This is incorrect, the function should not take a pointer, instead it should be written
as:
func (t T) String() string {
return fmt.Sprintf("%d/%g/%q", t.a, t.b, t.c)
}
fmt.Printf("%v\n", t)
The tutorial has the same issue, but the specification is correct.
I checked to see how String() method is written in packages. See
http://golang.org/src/pkg/image/geom.go?s=1791:1825#L69 for an example.
As a beginner I found this hard to debug.
The text was updated successfully, but these errors were encountered:
Here is a test. Should fail with Pointer type
package main
import (
"fmt"
"testing"
)
type Foo struct {
a, b int
}
func (foo *Foo) String() string {
return "FOO"
}
func TestString1(t *testing.T) {
f := Foo{1,2}
s := f.String()
if s != "FOO" {
t.Error("f.String() returned: " + s)
}
}
func TestString2(t *testing.T) {
f := Foo{1,2}
s := fmt.Sprintf("Foo: %v", f)
if s != "Foo: FOO" {
t.Error("f.String() returned: " + s)
}
}
by christopher.helck:
The text was updated successfully, but these errors were encountered: