Found 1082 Articles for Go Programming

Golang program to delete a file

Akhil Sharma
Updated on 22-Feb-2023 14:42:41

3K+ Views

Golang has os package that is used for deleting a file. In this os package the filename that we wish to delete is initially specified. The file is then deleted using the os.Remove method from the os package.In this article we are using two example to demonstrate the process of deleting a file. Syntax os.Remove() The os.Remove function in Go is used to delete a file or directory identified by a file path. The file or directory path that you want to remove is the only argument for this function. Algorithm Step 1 − Create a package main ... Read More

Golang program to copy one file into another file

Akhil Sharma
Updated on 22-Feb-2023 14:40:05

2K+ Views

In Golang, we can use Os Packages and IO packages to copy data from one file to another file. n the first method, we'll use OS packages like os.open, os.create and os.copy function. Whereas in the second method, we will use ioutill.Readfile and ioutil.Writefile to copy the files. Method1: Using OS Package In this illustration, the program initially uses the os.Open function to open the source file, source.txt. Then it uses the os.Generate function to create the destination file, destinaton.txt. Then, io.Copy function is used to copy the contents of the source file to the target file. Syntax os.Open ... Read More

Golang Program to rename a specified file by another name

Akhil Sharma
Updated on 22-Feb-2023 14:35:53

1K+ Views

Rename() is one of the internal function of golang used for renaming a specific file. Here we have used three examples in the first one we are using the Rename() function present in os package while in the second example we are using TempFile() function present in the ioutil package respectively. Method 1: Using OS Package In this method, we will write a go language program to rename a specified file by another name by using the Rename() function present in os package. Syntax func Rename(oldpath, newpath string) error The Rename() function is present in os package and ... Read More

Golang Program to check a specified file is exist or not

Akhil Sharma
Updated on 22-Feb-2023 14:34:59

377 Views

In golang, we have internal function like stat(), Open(0 and Readfile() that is used for checking whether a specific file exist or not. In the first example we are using the stat() method present in the os package while in the second and third example we are using Open() and ReadFile() function respectively. Method 1: Using STAT() Method In this method, we will use the Stat function present in os package to check if the file exists or not. Syntax Stat(file string) The stat() function is present in os package and is used to get the status of ... Read More

Golang Program to check a file has writable permission or not

Akhil Sharma
Updated on 22-Feb-2023 14:33:43

2K+ Views

In Golang, we can use openfile() and Stat() function to check whehter a file has writable permission or not. Here we have written two examples. in the first one we are using OpenFile() method present in the os package while in the second one we are using the Stat() method present in the os package respectively. Method 1: Using OpenFile() Method In this method, we will use the OpenFile() function to check the write permission of a file. This function opens a file with the given name and options, and returns a pointer to a File structure. Syntax func OpenFile(name ... Read More

Golang Program to check a directory is exist or not

Akhil Sharma
Updated on 22-Feb-2023 14:27:40

4K+ Views

In golang we have different inbuild function like Stat(), Open() and mkdir() to check whether a directory exist or not . Here we are using three functions. in the first one we are using the Stat() function present in the os package while in the second and third example we are using Open() and mkdir() function respectively to implement the result. Method 1: Using os.stat() Method The os package provides the Stat() function to get information about a file or directory. If the directory exists, the function returns a FileInfo structure, otherwise, it returns an error. Syntax func ... Read More

Golang Program to remove a specified directory

Akhil Sharma
Updated on 22-Feb-2023 14:25:39

3K+ Views

Golang provides several methods to remove a specified directory, including using the os and filepath packages. Removing a directory is a critical operation, and caution should be exercised when performing this task. This article will discuss the different methods to remove a directory in Golang, along with the syntax and algorithm for each method. Method 1: Using The os Package The os package in Golang provides several functions to perform operating system-related operations, including removing a directory. The Remove function of the os package is used to remove a directory. Syntax Remove(dirName) The Remove() function is present in os ... Read More

Golang Program to get the list the name of files in a specified directory

Akhil Sharma
Updated on 22-Feb-2023 14:24:24

2K+ Views

In Golang, we have two internal functions - ReadDir() and Walk function to get a list of filenames prenent in a specific directory. Here we have write three examples. In the first example We will use the ReadDir() function present in ioutil package while in the second one we are using the walk function present in the filepath package. Method 1: Using ioutil PACKAGE The ioutil package in Golang provides several functions to perform input/Output operations, including reading the files and directories. The ReadDir() function of the ioutil package is used to get the list of files in a specified ... Read More

Golang Program to print the home directory of the current user

Akhil Sharma
Updated on 22-Feb-2023 14:23:29

869 Views

The Go programming language provides various methods to obtain the home directory of the current user. This information can be useful in many applications, such as file management, system configuration, etc. In this article, we will discuss different methods to get the home directory in Go along with syntax and examples. Method 1: Using os.UserHomeDir() The os.UserHomeDir() function is part of the Go standard library and is the simplest and most efficient method to get the home directory of the current user. Here is an example that demonstrates the usage of os.UserHomeDir() Syntax func UserHomeDir() (string, error) The UserHomeDir() ... Read More

Golang Program to get the List of filenames matching the specified wild card pattern

Akhil Sharma
Updated on 22-Feb-2023 14:20:48

2K+ Views

One of the features of Go is the ability to search and retrieve files based on a specified pattern. In this article, we will discuss different methods to get the list of filenames that match a specified wildcard pattern in Go, and provide syntax and examples for each method. Method 1: Using the "filepath.glob" The "filepath.Glob" method is the easiest and most straightforward way to retrieve the list of filenames that match a specified wildcard pattern. The function takes a pattern as input and returns a slice of strings that match the pattern. Syntax filenames, err := filepath.Glob(pattern) The ... Read More

Advertisements