Go (golang) Advanced Tutorials - File I/O using ioutil package , Temporary File, Temporary Directory

Published: 18 February 2021
on channel: Ambasoft Java
1,089
19

#golang #go #ioutil #file



WriteFile with permissions using ioutil package
--------------------------------------------------------------------------

filedemo.go
------------------

package main
import(
"io/ioutil"
)
func main(){
data := []byte("Welcome")
err := ioutil.WriteFile("/home/ambasoft/Desktop/sample.txt",data,0700)
//0100 - Execute only
//0200 - Write only
//0300 - Write and Execute
//0400 - Read only
//0500 - Read and Execute
//0600 - Read and Write
//0700 - Read, Write and Execute
if(err != nil) {
panic(err)
}
}



Temporary Directory
--------------------------------

tempdemo.go
----------------------
package main
import (
"io/ioutil"
"os"
)
func main(){
dir,err := ioutil.TempDir("/home/ambasoft/Desktop","mytemp")
if(err != nil){
panic(err)
}
defer os.RemoveAll(dir) //Remove directories manually
}


Temporary File
-------------------------
tempfiledemo.go
---------------------------
package main
import (
"io/ioutil"
"os"
)
func main(){
f,err := ioutil.TempFile("/home/ambasoft/Desktop","mytemp")
if(err != nil){
panic(err)
}
defer os.Remove(f.Name()) //Remove file manually
}


Watch video Go (golang) Advanced Tutorials - File I/O using ioutil package , Temporary File, Temporary Directory online, duration hours minute second in high quality that is uploaded to the channel Ambasoft Java 18 February 2021. 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 1,089 times and liked it 19 visitors.