string and fmt.Stringer

4,178 views
Skip to first unread message

Kevin Albrecht

unread,
Mar 23, 2012, 9:34:28 AM3/23/12
to golan...@googlegroups.com
Is there a reason why string does not implement fmt.Stringer? It seems like this would be a useful abstraction.

Regards,
Kevin Albrecht

Jan Mercl

unread,
Mar 23, 2012, 10:19:48 AM3/23/12
to golan...@googlegroups.com
On Friday, March 23, 2012 2:34:28 PM UTC+1, Kevin Albrecht wrote:
Is there a reason why string does not implement fmt.Stringer? It seems like this would be a useful abstraction.

fmt.Stringer() returns a string representation of its receiver. Why would you want a string representation of a string when it's the same thing? In other words, is there some specific problem with this you are trying to solve?

Brad Fitzpatrick

unread,
Mar 23, 2012, 10:22:06 AM3/23/12
to Kevin Albrecht, golan...@googlegroups.com
On Fri, Mar 23, 2012 at 6:34 AM, Kevin Albrecht <only...@gmail.com> wrote:
Is there a reason why string does not implement fmt.Stringer? It seems like this would be a useful abstraction.

The largest reason is that no built-in types implement any methods.

Once string has String(), should it also have Split(...)? It gets slippery.

Kevin Albrecht

unread,
Mar 23, 2012, 10:24:06 AM3/23/12
to Jan Mercl, golan...@googlegroups.com
>> Is there a reason why string does not implement fmt.Stringer? It seems
>> like this would be a useful abstraction.
>
> fmt.Stringer() returns a string representation of its receiver. Why would
> you want a string representation of a string when it's the same thing? In
> other words, is there some specific problem with this you are trying to
> solve?

For example, I would like a function that does something to an object
that can be treated as a String:
func Log(s Stringer) { ... }

But right now I would need two implementations-- one that accepts
strings and one that accepts Stringer.

--Kevin

Jan Mercl

unread,
Mar 23, 2012, 10:30:19 AM3/23/12
to golan...@googlegroups.com
On Friday, March 23, 2012 3:24:06 PM UTC+1, Kevin Albrecht wrote:

For example, I would like a function that does something to an object
that can be treated as a String:
func Log(s Stringer) { ... }

But right now I would need two implementations-- one that accepts
strings and one that accepts Stringer.

type stringer string

func (s stringer) String() string { return string(s) } 

func foo {
        Log(stringer(myString))
}


Kevin Albrecht

unread,
Mar 23, 2012, 10:49:18 AM3/23/12
to Jan Mercl, golan...@googlegroups.com
Thanks, Jan... that seems like a pretty good solution.
--Kevin

2012/3/23 Jan Mercl <0xj...@gmail.com>:

Vanja Pejovic

unread,
Mar 23, 2012, 10:56:47 AM3/23/12
to Kevin Albrecht, golang-nuts, Jan Mercl

Alternatively, just have it only take a string call it like this:

Log(value.String())

Rémy Oudompheng

unread,
Sep 16, 2012, 3:06:19 PM9/16/12
to stett...@gmail.com, golan...@googlegroups.com
On 2012/9/16 <stett...@gmail.com> wrote:
> I wanted to use arbitrary objects as identifiers, which is somewhat a
> similar situation. I worked around the problem with the following code:
>
> type Identifier struct {
> id interface{}
> }
>
> func (id Identifier) String() string {
> switch t := id.id.(type) {
> default:
> return fmt.Sprint(t)
> }
> panic("Unkown Type as identifier") //Should never be reached
> }

Why not: func (id Identifier) String() string { return fmt.Sprint(id.id) }
?

Rémy.

Christian Dietrich

unread,
Sep 16, 2012, 3:38:43 PM9/16/12
to golan...@googlegroups.com, stett...@gmail.com
On Sunday, September 16, 2012 9:06:27 PM UTC+2, Rémy Oudompheng wrote:

Why not: func (id Identifier) String() string { return fmt.Sprint(id.id) } 
 
./base-os.go:10: cannot use id.id (type interface {}) as type string in function argument: need type assertion

Since id.id has no function in its interface. The type switch is a trick to work around the fact that string doesn't implement String()

Rémy Oudompheng

unread,
Sep 16, 2012, 3:54:19 PM9/16/12
to Christian Dietrich, golan...@googlegroups.com
On 2012/9/16 Christian Dietrich <stett...@gmail.com>:
> On Sunday, September 16, 2012 9:06:27 PM UTC+2, Rémy Oudompheng wrote:
>>
>>
>> Why not: func (id Identifier) String() string { return fmt.Sprint(id.id) }
>
>
> ./base-os.go:10: cannot use id.id (type interface {}) as type string in
> function argument: need type assertion

fmt.Sprint takes interface{} arguments, you must have written something else.

> Since id.id has no function in its interface. The type switch is a trick to
> work around the fact that string doesn't implement String()

A type switch with only a default case is not different from only the
default case contents, except for the scopes.

Rémy.

Christian Dietrich

unread,
Sep 17, 2012, 10:51:14 AM9/17/12
to golan...@googlegroups.com, Christian Dietrich


On Sunday, September 16, 2012 9:54:27 PM UTC+2, Rémy Oudompheng wrote:
fmt.Sprint takes interface{} arguments, you must have written something else.

I confused fmt.Sprint and fmt.Sprintf in my code. In the later case you can of course not use interface{} as a first argument. So you're right. 
Reply all
Reply to author
Forward
0 new messages