LEo
LEo

Reputation: 462

how to get the unique identifier of the current host in golang?

I want to get the unique identifier of the current host that is used as the license name in golang. How to do that ? For example, like C:

gethostid() //can get the host id

Upvotes: 3

Views: 8580

Answers (3)

Chaitanya Kulkarni
Chaitanya Kulkarni

Reputation: 1

Using this code you can generate a license for host using uuid of host

package main

import (
        "encoding/base64"
        "fmt"
        "github.com/google/uuid"
)

func main() {

        uuid := uuid.New()
        uuidBytes := uuid[:]
        licenseKeyBytes := append(uuidBytes)
        licenseKey := base64.StdEncoding.EncodeToString(licenseKeyBytes)
        fmt.Println("Generated license key:", licenseKey)
}

Upvotes: 0

Denis Brodbeck
Denis Brodbeck

Reputation: 133

You probably want the machine-id.

http://man7.org/linux/man-pages/man5/machine-id.5.html says:

The machine ID is usually generated from a random source during system installation and stays constant for all subsequent boots. Optionally, for stateless systems, it is generated during runtime at early boot if it is found to be empty.

The machine ID does not change based on local or network configuration or when hardware is replaced. Due to this and its greater length, it is a more useful replacement for the gethostid(3) call that POSIX specifies.

You can get the machine-id on (recent) Linux systems with:

cat /etc/machine-id
# or
cat /var/lib/dbus/machine-id

Most major OSs have a unique host identifier. Still, there may be non-unique host IDs (caused by imaging/cloning/backup-restore).

You can also check out my golang package machineid for implementation details, which works on BSD, Linux, OS X and Windows and requires no admin privileges.

Upvotes: 6

Marcel Lanz
Marcel Lanz

Reputation: 49

gethostid(3) is a UNIX/BSD specific libc function. reading from /etc/hostid would not work on non UNIX systems and is not platform independent.

since go does not provide something like gethostid() why not implement it like other platform independent languages like JAVA do, answered here: How to get a unique computer identifier in Java (like disk id or motherboard id)

Upvotes: 1

Related Questions