Get Relative Path from Two Absolute Paths in Golang

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

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

Get File Name from Absolute Path in Go

Akhil Sharma
Updated on 22-Feb-2023 14:49:18

11K+ 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

Difference Between Testing and Checking

Kiran Kumar Panigrahi
Updated on 22-Feb-2023 14:48:29

1K+ Views

In general, we can say that both Testing and Checking are used to evaluate a software product to make sure it behaves as expected. Both these processes are performed before the delivery of the product to ensure the delivery of correct and genuine product. Read this article to learn more about Testing and Checking and how they are different from each other. What is Testing? Testing is defined as a process of validating a product along with learning about that product by investigating and exploring the product during the process. Thus, testing is the process that motivate the tester to ... Read More

Golang Program to Create Directories

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

2K+ 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

Delete Empty and Non-Empty Directory in Go

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

2K+ 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

796 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

Golang Program to Delete a File

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

5K+ 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

Go Program to Copy One File into Another File

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

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

Difference Between Testing and Debugging

Kiran Kumar Panigrahi
Updated on 22-Feb-2023 14:36:14

3K+ Views

Both Testing and Debugging are important steps during and after the development of any software or application developed in any programming language. On the basis of their features and methods of practice, we can distinguish between Testing and Debugging. Read this article to learn more about Testing and Debugging and how they are different from each other. What is Testing? Testing is a process to check if a software application is working properly or not. The primary goal of testing is to find the errors and bugs in an application. The person who performs the testing is known as ... Read More

Rename a Specified File by Another Name in Go

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

Advertisements