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 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?