The post Change file permissions in Go (Golang) appeared first on Welcome To Golang By Example.
]]>func Chmod(name string, mode FileMode) error
Code
package main
import (
"fmt"
"log"
"os"
)
func main() {
// Create new file
new, err := os.Create("new.txt")
if err != nil {
log.Fatal(err)
}
defer new.Close()
stats, err := os.Stat("new.txt")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Permission File Before: %s\n", stats.Mode())
err = os.Chmod("new.txt", 0700)
if err != nil {
log.Fatal(err)
}
stats, err = os.Stat("new.txt")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Permission File After: %s\n", stats.Mode())
}
Output:
Permission File Before: -rw-r--r--
Permission File After: -rwx------
The post Change file permissions in Go (Golang) appeared first on Welcome To Golang By Example.
]]>The post Make a copy of a file in Go (Golang) appeared first on Welcome To Golang By Example.
]]>io.Copy() can be used to create a copy of the file from src to dest. A successful copy will return err != nil instead of err == EOF
Below is the signature of the function. It returns the number of bytes written to the dest
func Copy(dst Writer, src Reader) (written int64, err error)
Code
First create a file named “original.txt” and write some content in it.
package main
import (
"fmt"
"io"
"log"
"os"
)
// Copy a file
func main() {
// Open original file
original, err := os.Open("original.txt")
if err != nil {
log.Fatal(err)
}
defer original.Close()
// Create new file
new, err := os.Create("new.txt")
if err != nil {
log.Fatal(err)
}
defer new.Close()
//This will copy
bytesWritten, err := io.Copy(new, original)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Bytes Written: %d\n", bytesWritten)
}
Output:
Bytes Written:
If the contents of the file is less then we can also read the contents of the file first and then write all the contents to the new file. See below code.
Here also first create a file named “original.txt” and write some content in it
package main
import (
"io/ioutil"
"log"
)
// Copy a file
func main() {
//Read all the contents of the original file
bytesRead, err := ioutil.ReadFile("original.txt")
if err != nil {
log.Fatal(err)
}
//Copy all the contents to the desitination file
err = ioutil.WriteFile("new.txt", bytesRead, 0755)
if err != nil {
log.Fatal(err)
}
}
Output
new.txt file will have same content as original.txt
The post Make a copy of a file in Go (Golang) appeared first on Welcome To Golang By Example.
]]>The post Rename file or folder in Go (Golang) appeared first on Welcome To Golang By Example.
]]>os.Rename() function can be used to rename a file or folder. Below is the signature of the function.
func Rename(old, new string) error
old and new can be fully qualified as well. If the old and new path doesn’t lie in the same directory then os.Rename() function behaves the same as moving a file or folder.
Below is code for renaming a file
package main
import (
"log"
"os"
)
func main() {
//Create a file
file, err := os.Create("temp.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
//Change permission so that it can be moved
err = os.Chmod("temp.txt", 0777)
if err != nil {
log.Println(err)
}
err = os.Rename("temp.txt", "newTemp.txt")
if err != nil {
log.Fatal(err)
}
}
Output
First, it will create a file named temp.txt in the current working directory. Then it will rename it to newTemp.txt
Below is code for renaming a folder
package main
import (
"log"
"os"
)
func main() {
//Create a directory
err := os.Mkdir("temp", 0755)
if err != nil {
log.Fatal(err)
}
err = os.Rename("temp", "newTemp")
if err != nil {
log.Fatal(err)
}
}
Output:
First it will create a folder named temp in current working directory. Then it will rename it to newTemp
The post Rename file or folder in Go (Golang) appeared first on Welcome To Golang By Example.
]]>The post Create an empty file in Go (Golang) appeared first on Welcome To Golang By Example.
]]>os.Create() can be used to create an empty file in go. The signature of the function is
func Create(name string) (*File, error)
Basically this function
package main
import (
"log"
"os"
)
func main() {
file, err := os.Create("emptyFile.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
}
Output:
Check the contents of the file. It will be empty
The post Create an empty file in Go (Golang) appeared first on Welcome To Golang By Example.
]]>The post Append to an existing file in Go (Golang) appeared first on Welcome To Golang By Example.
]]>Let’s see an example. In the below program:
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
)
func main() {
//Write first line
err := ioutil.WriteFile("temp.txt", []byte("first line\n"), 0644)
if err != nil {
log.Fatal(err)
}
//Append second line
file, err := os.OpenFile("temp.txt", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer file.Close()
if _, err := file.WriteString("second line"); err != nil {
log.Fatal(err)
}
//Print the contents of the file
data, err := ioutil.ReadFile("temp.txt")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
}
Output:
first line
second line
The post Append to an existing file in Go (Golang) appeared first on Welcome To Golang By Example.
]]>The post Touch a file in Go (Golang) appeared first on Welcome To Golang By Example.
]]>package main
import (
"fmt"
"log"
"os"
"time"
)
func main() {
fileName := "temp.txt"
_, err := os.Stat(fileName)
if os.IsNotExist(err) {
file, err := os.Create("temp.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
} else {
currentTime := time.Now().Local()
err = os.Chtimes(fileName, currentTime, currentTime)
if err != nil {
fmt.Println(err)
}
}
}
Output:
When running the first time it will create the file. After the first time, it will update the modified time of the file.
The post Touch a file in Go (Golang) appeared first on Welcome To Golang By Example.
]]>The post Read a large file Line by Line in Go (Golang) appeared first on Welcome To Golang By Example.
]]>This is an example
to show how
to read file
line by line.
Here is the program:
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main(){
LinebyLineScan()
}
func LinebyLineScan() {
file, err := os.Open("./sample.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
Output:
This is an example
to show how
to read file
line by line.
Note however bufio.Scanner has max buffer size 64*1024 bytes which means in case you file has any line greater than the size of 64*1024, then it will give the error
bufio.Scanner: token too long
The post Read a large file Line by Line in Go (Golang) appeared first on Welcome To Golang By Example.
]]>