空的返回语句在主函数中的含义是什么?

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

What do empty returns in the main function mean?

问题

我从一个API(https://api.magiceden.dev/)复制粘贴了这段代码。这段代码获取链接并打印一个切片。以下是代码:

func main() {
	url := "https://api-mainnet.magiceden.dev/v2/wallets/6xX3z7uxTNB68izZW2GHKnzno49dizqeVVc5ncVzdjFM/activities?offset=0&limit=100"
	method := "GET"

	client := &http.Client{}
	req, err := http.NewRequest(method, url, nil)

	if err != nil {
		fmt.Println(err)
		return
	}
	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(body))
}

我对Go语言还不熟悉,我知道在其他函数中可以使用空的返回语句,但是在main函数中返回了什么呢?这就是问题所在,我还没有找到答案。

我尝试过在Google上搜索,但是找不到关于在main函数中使用空的返回语句的信息或示例。

英文:

I copy-pasted the code from an API (https://api.magiceden.dev/). This code gets the link and prints a slice. Here's the code:

func main() {
	url := "https://api-mainnet.magiceden.dev/v2/wallets/6xX3z7uxTNB68izZW2GHKnzno49dizqeVVc5ncVzdjFM/activities?offset=0&limit=100"
	method := "GET"

	client := &http.Client{}
	req, err := http.NewRequest(method, url, nil)

	if err != nil {
		fmt.Println(err)
		return
	}
	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(body))
}

I'm new to Go, and I know about empty return statements in other functions, but what is returned in main function? That's the question and I still haven't found the answer.

I tried googling it, but I couldn't find any info or examples of empty return statements in main function.

答案1

得分: 3

当函数签名中没有返回类型时,该函数中的return语句只会在此处停止函数的处理。然后不会再执行任何其他语句,但已注册的defer函数会按照它们注册的相反顺序进行处理。

英文:

When there is no return type in the function signature the return in such a function just stops the processing of the function at this point. No further statement are run then, but the registered defer functions are processed in the reverse order they have been registered.

huangapple
  • 本文由 发表于 2023年2月11日 22:04:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75420709.html
匿名

发表评论

匿名网友

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

确定