Go (golang) Tutorials - Command Line Arguments, File I/O

Published: 04 April 2018
on channel: Ambasoft Java
3,302
42

#golang #go #input #output #file

Command line Arguments & File I/O
------------------------------------
= Command Line Arguments
All the command line arguments will be collected in the os.Args array.
Args[0] will always contain the binary executable runtime name with the path
Args[1],Args[2],etc will be used to store the command line arguments in their
given order
File Output
WriteFile - Will always create a new file with the given name and content with the permission
provided

cmddemo.go
----------
package main
import ("fmt"
"os"
)
func main(){
fmt.Println(len(os.Args)) //by default Args array will contain the program runtime as
//command line index 0
fmt.Println(os.Args[1])
}

writedemo.go
------------
package main
import (
"io/ioutil"
)
func main(){
data:=[]byte("Hello World\n")
ioutil.WriteFile("myfile",data,0644) //0644 is a read/write permission for a file
//Create or replace the myfile with the given data
}

readdemo.go
-----------
package main
import ("fmt"
"io/ioutil"
)
func main(){
data,err:=ioutil.ReadFile("myfile") //Reads data from file, if any errors found returns back
//If an error occurred raise a panic, if not just proceed printing the data
if err!=nil {
panic(err)
}
fmt.Println(string(data))
}


Watch video Go (golang) Tutorials - Command Line Arguments, File I/O online, duration hours minute second in high quality that is uploaded to the channel Ambasoft Java 04 April 2018. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 3,302 times and liked it 42 visitors.