Re: converting an integer to string

2,271 views
Skip to first unread message

Ugorji Nwoke

unread,
Jun 1, 2012, 10:06:28 AM6/1/12
to golan...@googlegroups.com
Look at strconv package.

On Friday, June 1, 2012 9:59:03 AM UTC-4, Jean-Sébastien Ney wrote:
Hello,

  I'm trying to save the pid (an integer) to a file :

file, _ := os.OpenFile("tmp/camo.pid", os.O_CREATE, 0600)
file.WriteString(string(os.Getpid()))
But the i can convert properly the integer to a string, i just get the ASCII caracter corresponding to the number

Thanks 

Jean-Sébastien Ney

unread,
Jun 1, 2012, 10:15:33 AM6/1/12
to golan...@googlegroups.com
strconv.FormatInt and strconv.Atoi convert a string to an integer.
Which function would do the opposite ?

Jan Mercl

unread,
Jun 1, 2012, 10:19:42 AM6/1/12
to Jean-Sébastien Ney, golan...@googlegroups.com
On Fri, Jun 1, 2012 at 4:15 PM, Jean-Sébastien Ney <jeanseba...@gmail.com> wrote:
strconv.FormatInt and strconv.Atoi convert a string to an integer.
Which function would do the opposite ?


(Did you read the package docs?)

Patrick Mylund Nielsen

unread,
Jun 1, 2012, 10:20:09 AM6/1/12
to Jean-Sébastien Ney, golan...@googlegroups.com
FormatInt is int to string. There is a shorthand, itoa:
http://golang.org/pkg/strconv/#Itoa

On Fri, Jun 1, 2012 at 4:15 PM, Jean-Sébastien Ney
<jeanseba...@gmail.com> wrote:

Jean-Sébastien Ney

unread,
Jun 1, 2012, 10:22:52 AM6/1/12
to golan...@googlegroups.com, Jean-Sébastien Ney
oh sorry.

Christian Himpel

unread,
Jun 1, 2012, 10:24:13 AM6/1/12
to Jean-Sébastien Ney, golan...@googlegroups.com
On Fri, Jun 1, 2012 at 3:59 PM, Jean-Sébastien Ney
<jeanseba...@gmail.com> wrote:
> file, _ := os.OpenFile("tmp/camo.pid", os.O_CREATE, 0600)
> file.WriteString(string(os.Getpid()))
> But the i can convert properly the integer to a string, i just get the ASCII
> caracter corresponding to the number

you can also use formatted printing (e.g. if you have more than just
the number):

fmt.Fprint(file, "%d", os.Getpid())

Roberto Teixeira

unread,
Jun 1, 2012, 10:11:24 AM6/1/12
to Jean-Sébastien Ney, golan...@googlegroups.com
Le vendredi 1 juin 2012, Jean-Sébastien Ney a écrit :
Hello,

  I'm trying to save the pid (an integer) to a file :

file, _ := os.OpenFile("tmp/camo.pid", os.O_CREATE, 0600)
file.WriteString(string(os.Getpid()))
But the i can convert properly the integer to a string, i just get the ASCII caracter corresponding to the number


You want to do strconv.Itoa(os.Getpid())  instead of string(...)

-rst.
Reply all
Reply to author
Forward
0 new messages