Trying to write a bit of go, I would like to create a sort of cat function in Golang:
package main
import (
"fmt"
"os"
"io/ioutil"
"log"
)
func main() {
// part to ask the question and get the input
fmt.Print("Which file would you like to read?: ")
var input string
fmt.Scanln(&input)
fmt.Print(input)
// part to give the output
f, err := os.Open(os.Args[1]) // Open the file
if err != nil {
log.Fatalln("my program broken")
}
defer f.Close() // Always close things open
bs, err := ioutil.ReadAll(f)
if err != nil {
log.Fatalln("my program broken")
}
// part to print the output
fmt.Printf("input", bs) // %s convert directly in string the result
}
But I get a go panic on execution and do not find more explicit information.
What I have done wrong?
How can I get more information about that error from the terminal?
$ go run gocat.go Which file would you like to read?: gocat.go gocat.gopanic: runtime error: index out of range
goroutine 1 [running]: panic(0x4b1840, 0xc42000a0e0) /usr/lib/go/src/runtime/panic.go:500 +0x1a1 main.main() /home/user/git/go-experimentations/gocat/gocat.go:23 +0x4ba exit status 2
You use
=
rather than:=
So here is from where your error comes from.