syscall.GetComputerName

Hello I’m new to Go and I learning by experimenting. Can anyone help me with the following code:

    package main

import "fmt"

import "syscall"
import "os"

func main() {
    x, err := os.Hostname()
    y := syscall.GetComputerName
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(x)
    fmt.Println(y)

}

I would like to know how to display the value stored at location returned by syscall.getcomputername, as you can see I’m already able to see the correct value if I use os.Hostname(), however I like to understand the syscall package better as I believe it is a more direct way of getting the same information back (correct me if Im wrong). With the code as it is I only get the address location.

Kind regards

func ComputerName() (string, error) is a function. When you print it now you print the pointer to the function. So call it as a function and get name and error in the same way as os.Hostname

1 Like

It is true that syscall provides a more direct interface to the underlying operating system, but be careful about using the syscall package. It varies based on the operating system. For example, syscall.GetComputerName() is available on Windows, but not on Linux.

See the comments at the beginning of the online documentation for syscall:

https://golang.org/pkg/syscall/

The os package is independent of platform. If you use that, your program can compile and run on any operating system that Go supports.

So a good strategy is to use os whenever you can, and use syscall (or golang.org/x/sys package) only when you really need to, knowing that for cross-platform support, you will need to write separate versions of that part of your program to support each operating system.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.