[go-nuts] Easy question: read file and print to stdout

2,343 views
Skip to first unread message

James Fisher

unread,
Apr 24, 2010, 6:27:17 AM4/24/10
to golan...@googlegroups.com
Hi all,


A question which should hopefully be very easy to answer: I want to read from a file (UTF-8), and print it to stdout.  What is the most trivial way of doing this?  Currently I have:


package main

import (
  "os"
  "fmt"
  )

func main() {
  f, _ := os.Open("test", os.O_RDONLY, 777)
  b := new([512]byte)
  f.Read(b)
  fmt.Print((*b).(string))
  }


So, to read from file I have to read it into a bytestring.  First problem: I have to know in advance the length of the file.  Is there no standard library function that will hide the complexity of dynamically increasing the range of the bytestring as it reads the whole file, then gives me the whole bytestring?

Second problem: how do I print a bytestring as a string?  I can print it formatted as an array of integers, but that's obviously useless.  Using the casting syntax .(string) tells me: "invalid type assertion: *b.(string) (non-interface type [512]uint8 on left)".  Fair enough; is there a toString() function? I can't see any.


James.

Joan Miller

unread,
Apr 24, 2010, 6:33:25 AM4/24/10
to golang-nuts
To read the file you use `ioutil.ReadFile()`

http://golang.org/pkg/io/ioutil/#ReadFile
> --
> Subscription settings:http://groups.google.com/group/golang-nuts/subscribe?hl=en

Noah Evans

unread,
Apr 24, 2010, 6:44:06 AM4/24/10
to golan...@googlegroups.com
package main

import (
"flag";
"os";
"fmt";
"io/ioutil"
)

/*
make && ./tostring
*/

func usage() {
fmt.Fprintf(os.Stderr, "usage: tostring [flags] [path ...]");
flag.PrintDefaults();
os.Exit(2);
}

func main() {
flag.Usage = usage;
flag.Parse();
data, err := ioutil.ReadFile("/etc/passwd")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(data))
os.Exit(0)
}

works for me

roger peppe

unread,
Apr 24, 2010, 6:51:03 AM4/24/10
to James Fisher, golan...@googlegroups.com
you could use io.Copy, e.g.

fd, err := os.Open("foo", os.O_RDONLY)
if fd != nil {
io.Copy(fd, os.Stdout)
}

this has the advantage that it doesn't use up buffer
space proportional to the size of the file.

James Fisher

unread,
Apr 24, 2010, 9:36:32 AM4/24/10
to golan...@googlegroups.com
Hi all,

I'm looking in the std lib for a get_parent() function taking a filepath.  An alternative to:

func get_parent(p string) string {
  d, _ := path.Split(strings.TrimRight(p,"/\\"))
  return d
}





On Sat, Apr 24, 2010 at 2:34 PM, James Fisher <jamesh...@gmail.com> wrote:
To clarify, I'm looking for an alternative to this kludge:

func get_parent(p string) string {
  d, _ := path.Split(strings.TrimRight(p,"/\\"))
  return d

  }

On Sat, Apr 24, 2010 at 2:07 PM, James Fisher <jamesh...@gmail.com> wrote:
How nice, I will do :).  Question: is there an OS-independent GetParentDirectory() anywhere in the standard lib?  The closest I can find is http://golang.org/pkg/path/#Split , where I can take the first return value.  This does not work, though, if given a path with a trailing slash -- Split("/foo/bar/") returns ("/foo/bar/", "").  I could of course just strip the final slash if present, then split it, but this seems like a lot of work for a primitive function.


On Sat, Apr 24, 2010 at 12:14 PM, Noah Evans <noah....@gmail.com> wrote:
Cool, good luck and post back with your programs. I'm always curious to see what people are doing with go. 


On Sat, Apr 24, 2010 at 1:12 PM, James Fisher <jamesh...@gmail.com> wrote:
Roger's is def useful, though in this case yours -- I'm working with the string from the file.  Printing was just for the testcase, and conveniently also taught me the string() usage.  If it's not obvious, I started with go yesterday. :)


On Sat, Apr 24, 2010 at 12:01 PM, Noah Evans <noah....@gmail.com> wrote:
Out of curiosity is my solution or Rog's what you were looking for?

Noah


On Sat, Apr 24, 2010 at 12:59 PM, James Fisher <jamesh...@gmail.com> wrote:
ah-HAH.  I knew there was something I'd missed -- the whole ioutil package :)

Many thanks.


James Fisher

unread,
Apr 24, 2010, 11:08:37 AM4/24/10
to Noah Evans, golan...@googlegroups.com
Ah!  Obvious.

On Sat, Apr 24, 2010 at 3:15 PM, Noah Evans <noah....@gmail.com> wrote:
Have you tried using path.Clean first?

Sent from my iPhone
Noah


Reply all
Reply to author
Forward
0 new messages