Q:

How to get the absolute path from a relative path in Golang?

0

In the Go programming language, to get the absolute path from a relative path – we use the Abs() function of path/filepath package. The Abs() function returns an absolute representation of the path. If the given path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute pathname for a given file is not guaranteed to be unique.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Syntax:

func Abs(path string) (string, error)

Consider the below Golang program demonstrating how to get the absolute path from a relative path?

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	// Getting absolute path of hello.go
	abs, err := filepath.Abs("./hello.go")

	// Printing if there is no error
	if err == nil {
		fmt.Println("Absolute path is:", abs)
	}
}

Output

Absolute path is: /hello.go

Note: The absolute path may different based on the path.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now