empty Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/empty/ Sun, 23 Feb 2020 18:30:04 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 empty Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/empty/ 32 32 159787465 Create an empty file in Go (Golang) https://vikasboss.github.io/create-empty-file-go/ https://vikasboss.github.io/create-empty-file-go/#respond Fri, 21 Feb 2020 15:06:25 +0000 https://vikasboss.github.io/?p=1415 Overview os.Create() can be used to create an empty file in go. The signature of the function is Basically this function Create a named file with mode 0666 It truncates the file...

The post Create an empty file in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

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

  • Create a named file with mode 0666
  • It truncates the file if it already exits
  • In case of path issue, it returns a Path error
  • It returns a file descriptor which can be used for both reading and write

Code:

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.

]]>
https://vikasboss.github.io/create-empty-file-go/feed/ 0 1415
Touch a file in Go (Golang) https://vikasboss.github.io/touch-file-golang/ https://vikasboss.github.io/touch-file-golang/#respond Thu, 16 Jan 2020 18:33:03 +0000 https://vikasboss.github.io/?p=1176 Touching a file means Create an empty file if the file doesn’t already exist If the file already exists then update the modified time of the file. Output: When running the first...

The post Touch a file in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Touching a file means

  • Create an empty file if the file doesn’t already exist
  • If the file already exists then update the modified time of the file.
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.

]]>
https://vikasboss.github.io/touch-file-golang/feed/ 0 1176