username Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/username/ Fri, 17 Apr 2020 20:24:13 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 username Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/username/ 32 32 159787465 Get current Username in Go (Golang) https://vikasboss.github.io/get-current-username-golang/ https://vikasboss.github.io/get-current-username-golang/#respond Fri, 17 Apr 2020 20:23:21 +0000 https://vikasboss.github.io/?p=2044 Overview ‘os/user’ package can be used to get the current user name. Let’s see a working code Code Output

The post Get current Username in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

‘os/user’ package can be used to get the current user name.

Let’s see a working code

Code

package main

import (
	"fmt"
	"log"
	"os/user"
)

func main() {

	user, err := user.Current()
	if err != nil {
		log.Fatalf(err.Error())
	}

	username := user.Username

	fmt.Printf("Username: %s\n", username)
}

Output

Username: "someusername"

The post Get current Username in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/get-current-username-golang/feed/ 0 2044