×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang os.Executable() Function with Examples

Golang | os.Executable() Function: Here, we are going to learn about the Executable() function of the os package with its usages, syntax, and examples.
Submitted by IncludeHelp, on November 16, 2021

os.Executable()

In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The Executable() function is an inbuilt function of the os package, it is used to get the path name for the executable that started the current process.

Note: There is no guarantee that the path is still pointing to the correct executable. If a symlink was used to start the process, depending on the operating system, the result might be the symlink or the path it pointed to. If a stable result is needed, path/filepath.EvalSymlinks might help. Executable returns an absolute path unless an error occurred. The main use case is finding resources located relative to an executable.

It accepts nothing and returns the path name for the executable that started the current process and <nil> if there is no error; error, otherwise.

Reference: Executable

Syntax

func Executable() (string, error)

Parameters

  • None

Return Value

The return type of the os.Executable() function is (string, error), it returns the path name for the executable that started the current process and <nil> if there is no error; error, otherwise.

Example

// Golang program to demonstrate the
// example of Executable() function

package main

import (
	"fmt"
	"os"
)

func main() {
	// Getting the path name for the executable
	// that started the current process
	str, err := os.Executable()

	// Printing the return types & values
	fmt.Printf("str: %T, %v\n", str, str)
	fmt.Printf("err: %T, %v\n", err, err)
}

Output:

str: string, /tmpfs/play
err: <nil>, <nil>

Golang os Package »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.