Found 1082 Articles for Go Programming

Golang program to read and print all files from zip file

Akhil Sharma
Updated on 22-Feb-2023 15:29:47

1K+ Views

Golang has packages like os, io, and Archie/zip that can be used to read and print all the files from a zipped file. A zip file is a compressed collection of a set of files/folders. The OS package is used for performing copy operations, the io pacakge is for reading and writing operations whereas the archive/zip package is for unzipping and zipping the file. In this article, we are going to learn about using all these packages to perform the copy and unzipping of the files and printing the same. Method 1: Using io and os package This program uses ... Read More

Golang program to read the content of a file line by line

Akhil Sharma
Updated on 22-Feb-2023 15:27:42

2K+ Views

In golang there are various internal packages such as bufio, os and io that can be used for reading the content of a file line by line. The bufio and os packages are used for opening and scanning the file using os.open and bufio.NewScanner function. The io package we are going to use ioutil.ReadFile to read the file from the given destination and the use the string function to display it in the output. Method 1: Using bufio and os package In this illustration, bufio.NewScanner is used to read line by line content and os.Open is used to open the ... Read More

Golang program to get all files present in a directory

Akhil Sharma
Updated on 22-Feb-2023 15:26:31

2K+ Views

Golang has OS package such as OS.open(), os.closs(), os.copy() and move more that are used for performing different operations with the external files. Similiary we have io package in golang that is used for performing read and write operations on the external file. Here in this article, we are going to use these two packages to print all the files in the given directory. Method 1: Using OS Package This program opens the provided directory and reads its contents using the os package. A slice of os.FileInfo objects, which are returned by the Readdir function and include details about each ... Read More

Golang program to append a string in an existing file

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

557 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
Updated on 22-Feb-2023 14:51:41

4K+ 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
Updated on 22-Feb-2023 14:50:38

3K+ 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
Updated on 22-Feb-2023 14:49:18

6K+ 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
Updated on 22-Feb-2023 14:48:16

1K+ 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 delete empty and non-empty directory

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

967 Views

In golang, we can use os and dir packages to delete a given directory. Here we are using two examples to demonstrate to deletion of an empty and non-empty directory using os.open, os.Removeall. Syntax os.Open This function is a part of os package. It is used to open a file to read. It takes one input i.e. the filename which will be opened. os.RemoveAll It removes the directory and its contents completely. The function takes the name of the directory as an argument. dr.ReaddirNames This function is useful to read the file names from a directory. ... Read More

Golang program to rename a file

Akhil Sharma
Updated on 22-Feb-2023 14:44:50

297 Views

In golang we can use internal function of OS package to rename a file in various ways. In this article we are going to demonstrate how to change a file name using os. create, os.rename and os.open functions. Method1: Using os.Rename Function In this example, the file oldname.txt is renamed to newname.txt using the Rename function. If the renaming does not occur, such as if the source file doesn't exist or if the destination file already exists, the function produces an error by panicking. The error will be nil if the renaming procedure is successful. Syntax os.Rename os.Rename is ... Read More

Advertisements