简体   繁体   中英

Cannot call non-function (type string)

I'm very green so go easy on me! After much googling I'm still at a loss as to how I can apply other people's solutions to my dilemma.

I'm trying pull an IP address from a struct and pass it into a function. Here is the relevant code:

type Host struct {
    hostName string
    hostIP   string
}

func main() {

    records, err := readData("targets.csv")

    if err != nil {
        log.Println(err)
    }

    for _, record := range records {

        host := Host{
            hostName: record[0],
            hostIP:   record[1],
        }

        fmt.Printf("Querying %s at %s\n", host.hostName, host.hostIP)

Erroring here:

        body, err := telemetry(host.hostIP)(err)  //cannot call non-function telemetry(host.hostIP) (type string)
        if err != nil {
            log.Fatal(err)
        }
    }
}


func telemetry(target string) string {
    for {
        time.Sleep(30 * time.Second)
        msg := traceHost(target)
        currentTime := time.Now().Unix()
        fmt.Println(currentTime, ",", msg)
    }
}

Thanks in advance.

This statement

body, err := telemetry(host.hostIP)(err)

says

  • Call the function telemetry , passing the value of the host.hostIP variable to it.

  • Take its return value (which should be a reference to a function), and call that, passing the value of the variable err to it.

  • Assign its return values to the variables body , and err .

You are confusing a function definition:

func telemetry(target string) string { . . . }

meaning

telemetry is a function that takes a single string argument and returns a string .

with a function call.

That statement should be simply

body, err := telemetery(host.hostIP)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM