英文:
Golang: "missing return at end of function" when sending queries for item in text file
问题
我有这样一段代码,其中我在文本文件的每一行发送GET请求。然而,当我尝试运行代码时,我收到一个错误,提示“函数末尾缺少返回”。我知道这很可能是因为我在最后一个括号之前返回了它,但是我需要该括号内的代码,因为它定义了行、请求和正文内容。
我的代码:
// 程序入口
func main() {
rep := 1000
results := make(chan string)
for i := 0; i < rep; i++ {
go func(num int) {
results <- request(num)
}(i)
}
for i := 0; i < rep; i++ {
fmt.Println(<-results)
}
}
func request(num int) string {
// 读取 names.txt 文件中的每一行
wordlist, err := readLines("assets/names.txt")
if err != nil {
log.Fatalf("readLines: %s", err)
}
for _, line := range wordlist {
// 发送请求
response, err := http.Get("https://accounts.com/" + line + "/userId")
if err != nil {
fmt.Printf("%s", err)
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
fmt.Printf("%s\n", string(contents))
return string(contents)
}
}
请注意,我只翻译了代码部分,其他内容不包括在内。
英文:
I have this code where I send get requests per line in a text file. However, when I'm trying to run the code I get an error saying "missing return at end of function". I know that this is most likely due to the fact that I'm returning it before the final bracket, but I need the code within that bracket as It's defining the lines, the requests and the body content.
My code:
// Routine
func main() {
rep := 1000
results := make(chan string)
for i := 0; i < rep; i++ {
go func(num int) {
results <- request(num)
}(i)
}
for i := 0; i < rep; i++ {
fmt.Println(<-results)
}
}
func request(num int) string {
// For name in names.txt
wordlist, err := readLines("assets/names.txt")
if err != nil {
log.Fatalf("readLines: %s", err)
}
for _, line := range wordlist {
// Send request
response, err := http.Get("https://accounts.com/" + line + "/userId")
if err != nil {
fmt.Printf("%s", err)
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
fmt.Printf("%s\n", string(contents))
return string(contents)
}
}
答案1
得分: 1
根据Go编程语言规范的规定:
为了帮助解释这个问题,我们可以将您的代码简化为:
package main
import "fmt"
func main() {
fmt.Println(request([]string{"foo"}))
}
func request(wordlist []string) string {
for _, line := range wordlist {
// 做一些操作
return line
}
}
可以通过在循环后面添加一个返回语句来修复错误:
package main
import "fmt"
func main() {
fmt.Println(request([]string{"foo"}))
}
func request(wordlist []string) string {
for _, line := range wordlist {
// 做一些操作
return line
}
return ""
}
需要添加额外的return
语句的原因是因为函数可能会传递一个空的切片。在这种情况下,for
循环内的代码永远不会执行,意味着没有终止语句(请注意,您可以用panic
替换return
)。如果readLines
返回一个空的切片,您的代码也会出现相同的情况。
值得注意的是,在这里使用循环并没有太多意义,因为您似乎只对第一个值感兴趣。您可以使用类似的方法实现相同的结果:
package main
import "fmt"
func main() {
fmt.Println(request([]string{"foo", "boo"}))
fmt.Println(request(nil))
}
func request(wordlist []string) string {
if len(wordlist) == 0 {
return "No Data"
}
line := wordlist[0]
// 做一些操作...
return line
}
我猜您实际上想处理所有的行,但很遗憾,您的问题没有提供足够的上下文来进一步帮助。
英文:
As per the Go Programming Language Specification
>If the function's signature declares result parameters, the function body's statement list must end in a terminating statement.
To assist in explaining the issue we can simplify your code to:
package main
import "fmt"
func main() {
fmt.Println(request([]string{"foo"}))
}
func request(wordlist []string) string {
for _, line := range wordlist {
// Do something
return line
}
}
The error can be fixed by adding a return after the loop:
package main
import "fmt"
func main() {
fmt.Println(request([]string{"foo"}))
}
func request(wordlist []string) string {
for _, line := range wordlist {
// Do something
return line
}
return ""
}
The reason that the extra return
is needed is because the function could be passed an empty slice. In that case the code within the for
is never executed meaning that there is no terminating statement (note that you could replace the return
with a panic
). The same applies in your code if readLines
returns an empty slice.
I think it's worth noting that using a loop here does not make a lot of sense because you appear to only be interested in the first value. You could achieve the same result with something like:
package main
import "fmt"
func main() {
fmt.Println(request([]string{"foo", "boo"}))
fmt.Println(request(nil))
}
func request(wordlist []string) string {
if len(wordlist) == 0 {
return "No Data"
}
line := wordlist[0]
// do something...
return line
}
I suspect that you actually want to process all of the lines but unfortunately your question does not provide sufficient context to assist further.
答案2
得分: 0
你的函数想要查找一个文本。所以在那个循环中:
在找到它后跳出循环,
然后返回该值。
(我稍微简化了一下)
result := ""
for _, line := range wordlist {
if found {
result = yourAnswer
break
}
}
return result
}
<details>
<summary>英文:</summary>
Your function wants to lookup a text.
So in that loop:
step out of the loop after you found it,
then return the value.
(I minimised it a bit)
result := ""
for _, line := range wordlist {
if found {
result = yourAnswer
break
}
}
return result
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论