file - Golang How to read input filename in Go -


i run go file on input.txt go program read input.txt file when type in go run command ie:

go run gofile.go input.txt 

i don't want put input.txt in gofile.go code since go file should run on input name not input.txt.

i try ioutil.readall(os.stdin) need change command to

go run gofile.go < input.txt 

i use package fmt, os, bufio , io/ioutil. possible without other packages?

please take @ package documentation of io/ioutil using.

it has function this: readfile()

func readfile(filename string) ([]byte, error) 

example usage:

func main() {     // first element in os.args program name,     // need @ least 2 arguments have file name argument.     if len(os.args) < 2 {         fmt.println("missing parameter, provide file name!")         return     }     data, err := ioutil.readfile(os.args[1])     if err != nil {         fmt.println("can't read file:", os.args[1])         panic(err)     }     // data file content, can use     fmt.println("file content is:")     fmt.println(string(data)) } 

Comments