英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论