简体   繁体   English

去,麻烦遍历数组

[英]go, trouble iterating over array

I can't access the values in an array somehow. 我无法以某种方式访问​​数组中的值。 Here is how the array is declared: 这是声明数组的方式:

messages := strings.Split(request_string, end_of_message_terminator)

But when I make a loop over this array, I get empty strings as values. 但是,当我对该数组进行循环时,我得到了空字符串作为值。 That's the loop: 那是循环:

for i, v := range messages {
    fmt.Printf("messages are (3) %q\n", messages)
    go func(){

        fmt.Printf("message is %s\n", messages[i])
        fmt.Printf("i is %s\n", i)
        fmt.Printf("v is %s\n", v)
        respond_to_message(messages[i], response_writer())
    }()


}

And that's the output: 这就是输出:

messages are (3) ["asti" ""]
messages are (3) ["asti" ""]
message is                  
i is %!s(int=1)             
v is                        
message is                  
i is %!s(int=1)             
v is               

Does anyone know what the problem is? 有人知道问题出在哪里吗? Why can I access the individual values in this slice? 为什么可以访问此片中的各个值?

Just in case you want to run this code, here is the entire program: 万一您想运行此代码,以下是整个程序:

package main


import (
    "fmt"
    "net"
    "os"
    "strings"
//  "io/ioutil"


)


func main() {

    end_of_message_terminator := "||"
    beginning_of_next_message := ""
    request := make([]byte, 512)

    service_port := ":7777"
    tcpAddr, err := net.ResolveTCPAddr("tcp4", service_port)
    checkError(err)
    listener, err := net.ListenTCP("tcp", tcpAddr)
    checkError(err)



    for {
        conn, err := listener.Accept()

        if err != nil {

            continue

        }

        read_len, err := conn.Read(request)

        if read_len == 0 {
            continue
        }

        request_string := string(request[:read_len])
        fmt.Printf("Request String '%s'\n", request_string)

        messages := strings.Split(request_string, end_of_message_terminator)
        fmt.Printf("messages are (1) %q\n", messages)

        messages[0] = beginning_of_next_message + messages[0]

        if messages[len(messages) - 1] != "" {
            beginning_of_next_message = messages[len(messages) - 1]
            messages[len(messages) - 1] = ""

        }

        if len(messages) == 1 {
            continue
        }

        fmt.Printf("messages are (2) %q\n", messages)
        for i, v := range messages {
            fmt.Printf("messages are (3) %q\n", messages)
            go func(){

                fmt.Printf("message is %s\n", messages[i])
                fmt.Printf("i is %s\n", i)
                fmt.Printf("v is %s\n", v)

            }()


        }
        conn.Close()

    }

}






func checkError(err error) {

    if err != nil {

        fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
        os.Exit(1)

    }

}

You need to run this program first: 您需要先运行此程序:

netcat -l -p 8000

And then, finally (after starting both this program and running netcat -l -p 8000 ) you need to run: 然后,最后(在启动该程序并运行netcat -l -p 8000 ),您需要运行:

printf "asti||"  | netcat localhost 7777

In your goroutine you are not binding i properly: 在您的goroutine中,您没有正确绑定i

for i, v := range messages {
    fmt.Printf("messages are (3) %q\n", messages)
    go func(i int){

        fmt.Printf("message is %s\n", messages[i])
        fmt.Printf("i is %s\n", i)
        fmt.Printf("v is %s\n", v)
        respond_to_message(messages[i], response_writer())
    }(i) // you'll have to pass i here
}

You don't see any output because for each the value of i gets set to len(messages) - 1 which in this case is 2 - 1 = 1 and the value messages[1] == "" 您看不到任何输出,因为对于每一个i的值都设置为len(messages) - 1 ,在这种情况下为2 - 1 = 1 ,而值messages[1] == ""

So each goroutine is printing messages[1] which is an empty string. 因此,每个goroutine都在打印messages[1] ,这是一个空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM