string(...) function

196 views
Skip to first unread message

simran

unread,
Apr 19, 2015, 9:34:08 PM4/19/15
to golan...@googlegroups.com
Hi All, 
 
Where can one find the definition of the string(...) function. 

I'm assuming it's a builtin?

I've tried various web searches like: golang [builtin] {functions,string}, etc, but so far i have only bee able to decude (which may be incorrect) from stackoverflow that: 

* func string(...) is builtin?
* It will call a "String(...)" function if one exists for that object/struct?

Would appreciate any pointers to where i can find official documentation for it. 

thanks,

simran.

Jesse McNelis

unread,
Apr 19, 2015, 9:37:17 PM4/19/15
to simran, golang-nuts
On Mon, Apr 20, 2015 at 11:33 AM, simran <simran...@gmail.com> wrote:
Hi All, 
 
Where can one find the definition of the string(...) function. 

I'm assuming it's a builtin?

It's not a function, it's a conversion.
 

I've tried various web searches like: golang [builtin] {functions,string}, etc, but so far i have only bee able to decude (which may be incorrect) from stackoverflow that: 

* func string(...) is builtin?
* It will call a "String(...)" function if one exists for that object/struct?

It won't.

simran

unread,
Apr 19, 2015, 11:24:11 PM4/19/15
to Jesse McNelis, golang-nuts
Thankyou Jesse. Much appreciated. Has helped clear a lot of my confusion :) 

roger peppe

unread,
Apr 20, 2015, 7:56:37 AM4/20/15
to simran, golang-nuts
> Would appreciate any pointers to where i can find official documentation for it.

You might find this useful: http://golang.org/pkg/builtin/
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Konstantin Khomoutov

unread,
Apr 20, 2015, 11:38:40 AM4/20/15
to simran, golan...@googlegroups.com
On Mon, 20 Apr 2015 11:33:09 +1000
simran <simran...@gmail.com> wrote:

> Where can one find the definition of the string(...) function.
[...]

> * It will call a "String(...)" function if one exists for that
> object/struct?

In additions to what others have said, this one works in Go the other
way round: the code receiving arbitrary values, which it intends to get
a textual representation of, might "type-assert" the value to an
interface which supports a method of getting that textual
representation.

For instance, the `fmt` standard package implements this approach by
having the Stringer interface [1]. Then, the implementations of
various Print* functions in that package can do something along these
lines:

func PrintAny(v interface{}) {
var s string
switch v.(type) {
case string:
s = v
case Stringer:
// OK, we can ask the value in `v` to give us its
// textual representation as its type sees fit, as it
// supports the `func String() string` method:
s = v.String()
default:
// Well, then use reflection to produce the textual
// representation of the value in `v`.
}
// Output whatever ended up in `s`.
}

All this is explained in detail in [2], which, I'd say, is a must-read
(in its entirety) for any newfangled gopher.

[...]

1. https://golang.org/pkg/fmt/#Stringer
2. https://golang.org/doc/effective_go.html#interface_conversions
Reply all
Reply to author
Forward
0 new messages