go - 发送单字节 net.Conn Go

标签 go

我有一个接受连接的 Go TCP 服务器,我想一次回显 1 个字节的消息,我看不到让 net.Conn 使用 net 发送单个字节的方法。康涅狄格州.Write

c.Write([]byte(b)) 无法将 b(byte 类型)转换为 []byte 类型

c.Write(b) 不能在 c.Write 的参数中使用 b(字节类型)作为 []byte 类型

最佳答案

io.Writer 始终接受 []byte 作为参数。使用 1 字节长的 byte slice 。您尝试的([]byte(b))是将单个字节转换为字节 slice 。相反,创建一个单元素字节 slice ,其中 b 作为唯一元素:

n, err := c.Write([]byte{b})

关于go - 发送单字节 net.Conn Go,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39901489/

相关文章:

go - 如何从golang项目文件中获取图片路径

go - `int * time.Second` 什么时候工作,什么时候不在 golang 中?

go - 用go编写更简洁的代码

go - 获取结构字段类型的简单字符串表示

windows - 如何从剪贴板内存 (uintptr) 中检索图像数据缓冲区?

go - 如何从 Go 中的结构中的接口(interface)实例获取属性

google-app-engine - 创建数据存储的成本。 key : Storing a key in struct versus an id and fetching from the datastore

Gorm加载相关数据

go - 分配空 slice 而不引用其类型?

go - 安装 Kubernetes Go 客户端