douzhuanfen5923 2016-07-14 17:17
浏览 24
已采纳

去,麻烦遍历数组

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
", messages)
    go func(){

        fmt.Printf("message is %s
", messages[i])
        fmt.Printf("i is %s
", i)
        fmt.Printf("v is %s
", 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'
", request_string)

        messages := strings.Split(request_string, end_of_message_terminator)
        fmt.Printf("messages are (1) %q
", 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
", messages)
        for i, v := range messages {
            fmt.Printf("messages are (3) %q
", messages)
            go func(){

                fmt.Printf("message is %s
", messages[i])
                fmt.Printf("i is %s
", i)
                fmt.Printf("v is %s
", 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:

printf "asti||"  | netcat localhost 7777
  • 写回答

1条回答 默认 最新

  • dongyou9818 2016-07-14 17:27
    关注

    In your goroutine you are not binding i properly:

    for i, v := range messages {
        fmt.Printf("messages are (3) %q
    ", messages)
        go func(i int){
    
            fmt.Printf("message is %s
    ", messages[i])
            fmt.Printf("i is %s
    ", i)
            fmt.Printf("v is %s
    ", 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] == ""

    So each goroutine is printing messages[1] which is an empty string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探