Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation: func String() description in "Effective Go" is wrong #1796

Closed
gopherbot opened this issue May 11, 2011 · 3 comments
Closed

Documentation: func String() description in "Effective Go" is wrong #1796

gopherbot opened this issue May 11, 2011 · 3 comments

Comments

@gopherbot
Copy link
Contributor

by christopher.helck:

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.
@rsc
Copy link
Contributor

rsc commented May 11, 2011

Comment 1:

Haven't looked to confirm this.  Might or might not be true.

Owner changed to @robpike.

Status changed to Accepted.

@gopherbot
Copy link
Contributor Author

Comment 2 by christopher.helck:

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)
    }
}

@robpike
Copy link
Contributor

robpike commented May 11, 2011

Comment 3:

This issue was closed by revision 89c59bc.

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc unassigned robpike Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants