James Fisher
unread,Apr 24, 2010, 6:27:17 AM4/24/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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.