Akhil Sharma

Akhil Sharma

507 Articles Published

Articles by Akhil Sharma

Page 28 of 51

Golang program to append a string in an existing file

Akhil Sharma
Akhil Sharma
Updated on 22-Feb-2023 1K+ Views

In Golang, we can use io and os packages to append a string in an existing file. file contains data that can be manipulated in many ways like editing and writing the data. In this article the first method will demonstrate the application of os.Open file of OS package. In the second method, we are going to demonstrate the application of io package. Method 1: Using OS Package In this method, we are going to use os.OpenFile function of OS package in Golang. The permissions for the file are specified by the 0644 parameters. The file will be created ...

Read More

Golang program to get the file extension

Akhil Sharma
Akhil Sharma
Updated on 22-Feb-2023 7K+ Views

In golang, we can use the path package and string package to get the extension of a particular file. Here, in his article, we will obtain the file extension using two methods. In the first method, we will use path package function path.Ext. In the second method, we will use strings package function strings.LastIndex. Method 1: Using Path Package In this method, we will use path.Ext from path package to get the file extension. This built-in function will take the file as an input whose extension is to be printed. Syntax path.Ext The Go path/filepath package makes it easier ...

Read More

Golang program to get the relative path from two absolute paths

Akhil Sharma
Akhil Sharma
Updated on 22-Feb-2023 5K+ Views

To get the relative path from two absolute paths in golang, we use filepath and string packages. Relative paths tell the location of file with respect to present working directory whereas absolute paths tell the location of file starting from root directory. In the first method we will use filepath package functions and in the second method we will use strings package function. Method 1: Using Filepath Package In this program, the base name of the file, which is the file name without the directory path, is extracted using the filepath.Base function from the path/filepath package. The name of the ...

Read More

Golang program to get the name of the file from the absolute path

Akhil Sharma
Akhil Sharma
Updated on 22-Feb-2023 12K+ Views

We are going to use the filepath and string functions of golang to get the file name from the absolute path. An absolute directory begins with the root directory and includes all intermediate directories. In the first method, we will use filepath package functions and in the second example, we will use strings package functions. Method 1: Using filepath Package The base name of the file, which is the file name without the directory path, is extracted in this program using the filepath.Base function from the path/filepath package. The name of the extracted file is then displayed on the console. ...

Read More

Golang program to create directories

Akhil Sharma
Akhil Sharma
Updated on 22-Feb-2023 3K+ Views

Golang has internal packages like os and io packages for creating a new directory. Here, we will create a directory using two examples. In the first example we will use os.Mkdir function and in the second example we will use ioutil.WriteFile function to execute the program. Method 1: Using os.Mkdir Function In this method, a directory named dirName variable is created using the os.Mkdir function. The permission bits for the new directory are the second input to os.Mkdir, which we set to 0755. (read, write, and execute permissions for the owner and read and execute permissions for others). The program ...

Read More

Golang program to copy one file into another file

Akhil Sharma
Akhil Sharma
Updated on 22-Feb-2023 4K+ 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 check a specified file is exist or not

Akhil Sharma
Akhil Sharma
Updated on 22-Feb-2023 1K+ 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
Akhil Sharma
Updated on 22-Feb-2023 3K+ 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
Akhil Sharma
Updated on 22-Feb-2023 8K+ 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
Akhil Sharma
Updated on 22-Feb-2023 5K+ 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
Showing 271–280 of 507 articles
« Prev 1 26 27 28 29 30 51 Next »
Advertisements