gpt4 book ai didi

websocket - 将 io 字符串转为 int Atoi 无效参数

转载 作者:数据小太阳 更新时间:2023-10-29 03:17:57 26 4
gpt4 key购买 nike

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

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

平台信息

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

我找不到启动此错误的代码,只能在定义错误代码的地方找到(取决于平台)。我很感激改进我的代码的一般想法,但主要是我只想解决转换问题。

这是否与我的缓冲区 -> 字符串转换和 slice 操作有关(我不想使用 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(根据文件结尾,它在其他操作系统上可能看起来不同)。可以通过为重新 slice 添加结束索引来修复此代码,方法如下:

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

最佳答案

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

我最好的猜测是你有一个 C 风格的以 null 结尾的字符串。例如,简化您的代码,

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 "".

我怀疑您的问题是您忽略了 ws.Readn 返回值。例如(包括诊断消息),我希望,

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())
}

关于websocket - 将 io 字符串转为 int Atoi 无效参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9240008/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com