0

我正在尝试从 Go 语言的 WebSockets 连接中解析一个字符串。我正在实现连接的两端,因此数据格式的规范仅取决于我。

由于这是一个简单的应用程序(通常用于学习目的),我想出了ActionId Data,其中 ActionId 是uint8. BackendHandler是 WebSocket Connection 中每个请求的处理程序。

平台信息

kuba:~$ echo {$GOARCH,$GOOS,`6g -V`}
amd64 linux 6g version release.r60.3 9516

代码:

const ( // Specifies ActionId's
  SabPause = iota
)

func BackendHandler(ws *websocket.Conn) {
  buf := make([]byte, 512)
  _, err := ws.Read(buf)
  if err != nil { panic(err.String()) }
  str := string(buf)
  tmp, _ := strconv.Atoi(str[:0])
  data := str[2:]
  fmt.Println(tmp, data)
  switch tmp {
    case SabPause:
      // Here I get `parsing "2": invalid argument`
      // when passing "0 2" to websocket connection
      minutes, ok := strconv.Atoui(data)
      if ok != nil {
        panic(ok.String())
      }
      PauseSab(uint8(minutes))
    default:
      panic("Unmatched input for BackendHandler")
  }
}

所有输出:(注意我用于检查的 Println)

0 2
panic: parsing "2": invalid argument [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference

我找不到启动此错误的代码,只有定义错误代码的位置(取决于平台)。我很欣赏改进我的代码的一般想法,但主要是我只想解决转换问题。

这与我的缓冲区-> 字符串转换和切片操作(我不想使用 SplitAfter 方法)有关吗?

编辑

此代码重现了该问题:

package main

import (
  "strconv"
  "io/ioutil"
)

func main() {
  buf , _ := ioutil.ReadFile("input")
  str := string(buf)
  _, ok := strconv.Atoui(str[2:])
  if ok != nil {
    panic(ok.String())
  }
}

该文件input必须包含0 2\r\n(取决于文件结尾,它在其他操作系统上可能看起来不同)。可以通过为 reslice 添加结束索引来修复此代码,这样:

_, ok := strconv.Atoui(str[2:3])
4

1 回答 1

1

您没有提供一个小的可编译和可运行的程序来说明您的问题。您也没有提供完整且有意义的打印诊断信息。

我最好的猜测是你有一个 C 风格的空终止字符串。例如,简化您的代码,

package main

import (
    "fmt"
    "strconv"
)

func main() {
    buf := make([]byte, 512)
    buf = []byte("0 2\x00") // test data
    str := string(buf)
    tmp, err := strconv.Atoi(str[:0])
    if err != nil {
        fmt.Println(err)
    }
    data := str[2:]
    fmt.Println("tmp:", tmp)
    fmt.Println("str:", len(str), ";", str, ";", []byte(str))
    fmt.Println("data", len(data), ";", data, ";", []byte(data))
    // Here I get `parsing "2": invalid argument`
    // when passing "0 2" to websocket connection
    minutes, ok := strconv.Atoui(data)
    if ok != nil {
        panic(ok.String())
    }
    _ = minutes
}

输出:

parsing "": invalid argument
tmp: 0
str: 4 ; 0 2 ; [48 32 50 0]
data 2 ; 2 ; [50 0]
panic: parsing "2": invalid argument

runtime.panic+0xac /home/peter/gor/src/pkg/runtime/proc.c:1254
    runtime.panic(0x4492c0, 0xf840002460)
main.main+0x603 /home/peter/gopath/src/so/temp.go:24
    main.main()
runtime.mainstart+0xf /home/peter/gor/src/pkg/runtime/amd64/asm.s:78
    runtime.mainstart()
runtime.goexit /home/peter/gor/src/pkg/runtime/proc.c:246
    runtime.goexit()
----- goroutine created by -----
_rt0_amd64+0xc9 /home/peter/gor/src/pkg/runtime/amd64/asm.s:65

如果您将我的打印诊断语句添加到您的代码中,您会看到什么?

请注意,您的tmp, _ := strconv.Atoi(str[:0])陈述可能是错误的,因为str[:0]相当于str[0:0],相当于空string ""

我怀疑您的问题是您忽略nws.Read. 例如(包括诊断消息),我希望,

buf := make([]byte, 512)
buf = buf[:cap(buf)]
n, err := ws.Read(buf)
if err != nil {
    panic(err.String())
}
fmt.Println(len(buf), n)
buf = buf[:n]
fmt.Println(len(buf), n)

另外,尝试使用此代码设置tmp

tmp, err := strconv.Atoi(str[:1])
if err != nil {
    panic(err.String())
}
于 2012-02-11T15:04:32.513 回答