语法错误:意外的名称,期望分号或换行符。

huangapple go评论92阅读模式
英文:

syntax error: unexpected name, expecting semicolon or newline

问题

我不明白为什么我的代码会有语法错误。

这段代码计算了 Docker 启动和停止之间的时间。
我使用协程来返回结束时间。

endtime := go ping(string(curl_out))

我认为这是错误的。我该如何使用关键字 go?

在 Go 中,我可以在函数体外声明语句吗?

英文:

I don't understand why my 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()
	for 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 and stop.
I use routine to return end time.

endtime := go ping(string(curl_out))

I think it's wrong. How can I use keyword go?

In Go, Do I decelerate statement outside function body?

答案1

得分: 3

有两个主要问题(以及许多无关的问题)。

首先,你需要给命名返回参数加上括号:

func ping(curl_out string) (endtime int64) {

其次,你不能给一个goroutine赋值返回值。它在一个全新的上下文中异步执行。使用通道在goroutine之间进行通信。

你可以这样声明ping函数:

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

然后传入一个通道来接收值:

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

(尽管在这种情况下使用goroutine没有意义,因为你想要同步获取值)

英文:

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

First, you need to parenthesize named return parameters

func ping(curl_out string) (endtime int64) {

Second, you can't assign the return value of a goroutine. It's executed asynchronously in an entirely new context. Use a channel to communicate between goroutines

You could declare the ping function like:

func ping(curl_out string, endtime chan&lt;- int64) {

And then pass in a channel on which to receive the value

ch := make(chan int64)
go ping(string(curl_out), ch)
endtime &lt;- ch

(Though there is no point in this case to using a goroutine, since you want the value synchronously)

答案2

得分: 1

在第9行:

添加括号

func ping(curl_out string) (endtime int64) {

或者删除返回值的名称:

func ping(curl_out string) int64 {

关于使用go关键字创建goroutine的问题,你是对的,你不能将其赋值给返回值,只需删除go关键字并直接赋值即可。

此外(风格上),Go语言的变量采用驼峰命名法,所以你应该将变量从run_container改为runContainer,或者更好地改为container,甚至更好地改为crun_curl也是同样的道理,可以改为curl

这是你的代码稍微改进后的版本(不太清楚它的功能):

http://play.golang.org/p/3Y7TPip5kP

英文:

In line 9:

Add parenthesis

func ping(curl_out string) (endtime int64) {

or remove return value name:

func ping(curl_out string) int64 {

And you're right about the use of go keyword to create a goroutine, you can't assign that to a return value, just remove the go keyword and assign the value directly.

Also (style) go variables are in camelCase so you should change your variables from run_container to either runContainer or better just container or even better just c, same goes for run_curl it could be curl

Here's an slightly enhanced version of your code ( without really knowing what it does)

http://play.golang.org/p/3Y7TPip5kP

huangapple
  • 本文由 发表于 2015年7月14日 23:54:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/31411555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定