go - syntax error: unexpected name, expecting semicolon or newline -


i don't understand why code has syntax error.

package main  import (     "fmt"     "os/exec"     "time" )  func ping(curl_out string) endtime int64 {     try_curl := exec.command("curl", "localhost:8500/v1/catalog/nodes")     try_curl_out := try_curl.output()     try_curl_out == curl_out {         try_curl := exec.command("curl", "localhost:8500/v1/catalog/nodes")         try_curl_out := try_curl.output()     }     endtime := time.now().unix()     return endtime }  func main() {     run_container := exec.command("docker", "run", "-p", "8400:8400", "-p", "8500:8500", "-p", "8600:53/udp", "-h", "node1", "progrium/consul", "-server", "-bootstrap")     container_id, err := run_container.output()     if err != nil {         fmt.println(err)         return     }     run_curl := exec.command("curl", "localhost:8500/v1/catalog/nodes")     curl_out, err := run_curl.output()     if err != nil {         fmt.println(err)         return     }     endtime := go ping(string(curl_out))     container_id, err = exec.command("docker", "stop", container_id)     if err != nil {         fmt.println(err)         return     }     startime := time.now().unix()     fmt.println("delay is", endtime-startime) }  # command-line-arguments ./main.go:9: syntax error: unexpected name, expecting semicolon or newline ./main.go:11: non-declaration statement outside function body ./main.go:15: non-declaration statement outside function body ./main.go:16: non-declaration statement outside function body ./main.go:17: non-declaration statement outside function body ./main.go:18: syntax error: unexpected } 

this code calculates time between docker start , stop. use routine return end time.

endtime := go ping(string(curl_out)) 

i think it's wrong. how can use keyword go?

in go, decelerate statement outside function body?

there's 2 primary issues (and many unrelated).

first, need parenthesize named return parameters

func ping(curl_out string) (endtime int64) { 

second, can't assign return value of goroutine. it's executed asynchronously in entirely new context. use channel communicate between goroutines

you declare ping function like:

func ping(curl_out string, endtime chan<- int64) { 

and pass in channel on receive value

ch := make(chan int64) go ping(string(curl_out), ch) endtime <- ch 

(though there no point in case using goroutine, since want value synchronously)


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -