user7421969
user7421969

Reputation: 41

Get value stored at address

I'm starting to learn GO and would like for someone to help me understand something. How can I read the value at address that is returned by syscall.GetcomputerName? I understand that that call will store the address in variable y. Thank you

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)

}

Upvotes: 1

Views: 457

Answers (1)

peterSO
peterSO

Reputation: 166559

syscall.GetComputerName is the address of the function. To execute the syscall.GetComputerName function use the function call operator (). For example, on Windows,

package main

import (
    "fmt"
    "syscall"
    "unicode/utf16"
)

func ComputerName() (name string, err error) {
    var n uint32 = syscall.MAX_COMPUTERNAME_LENGTH + 1
    b := make([]uint16, n)
    e := syscall.GetComputerName(&b[0], &n)
    if e != nil {
        return "", e
    }
    return string(utf16.Decode(b[0:n])), nil
}

func main() {
    name, err := ComputerName()
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println("ComputerName:", name)
}

Output:

ComputerName: PETER

Microsoft: GetComputerNameW

Upvotes: 4

Related Questions