Function Golang数据包结构返回缓冲区

Function Golang数据包结构返回缓冲区,function,struct,go,byte,packet,Function,Struct,Go,Byte,Packet,我制作了一个包,里面有一个包结构,如下所示: //A packet buffer object package Packet import ( "bytes" "encoding/binary" ) type Packet struct { buffer bytes.Buffer } func (p Packet) GetBytes() []byte { return p.buffer.Bytes() } func (p Packet) AddString

我制作了一个包,里面有一个包结构,如下所示:

//A packet buffer object
package Packet

import (
    "bytes"
    "encoding/binary"
)

type Packet struct {
    buffer bytes.Buffer
}

func (p Packet) GetBytes() []byte {
    return p.buffer.Bytes()
}

func (p Packet) AddString(s string) {
    p.buffer.Write([]byte(s))
}

func (p Packet) AddInt(i_ int) {
    //Convert int to byte
    b := make([]byte, 2)
    binary.LittleEndian.PutUint16(b, uint16(i_))
    //Push byte to buffer
    p.buffer.Write([]byte(b))
}

func (p Packet) AddByte(b []byte) {
    p.buffer.Write(b)
}
这是一个会话包,它使用数据包结构来形成数据包并将其发送到客户端

package Session

type MapleSession struct {
    connection net.Conn
    EncryptIV, DecryptIV []byte
    isConnected bool
}

func (session *MapleSession) Run(conn net.Conn) { 
    //Display where the new connection is coming from
    session.connection = conn
    fmt.Println("Client connected from:", session.connection.RemoteAddr())

    //Set the user connected variable on
    session.isConnected = true

    //Send Handshake
    packet := MaplePacket.CreateHandShake(&session.EncryptIV, &session.DecryptIV, 40, "", []byte("0x05"))
    session.connection.Write(packet)
}
这是一个MaplePacket包,它创建从会话包请求的数据包发送到客户端

package MaplePacket

func CreateHandShake (eIV, dIV *[]byte, version int, location string, locale []byte) []byte{
    packet := Packet.Packet{}

    //Create IVs
    *eIV = (make([]byte, 4))
    n1, _ := rand.Read(*eIV)
    *dIV = (make([]byte, 4))
    n2, _ := rand.Read(*dIV)

    if (n1 + n2 < 8) {
        fmt.Println("Error in IV generation")
    }    

    //Create the packet
    packet.AddInt(version)
    packet.AddString(location)
    packet.AddByte(*dIV)
    packet.AddByte(*eIV)
    packet.AddByte(locale)

    fmt.Println(packet.GetBytes())

    return packet.GetBytes()
}
package包
func CreateHandShake(eIV,dIV*[]字节,version int,location字符串,locale[]字节)[]字节{
数据包:=数据包。数据包{}
//创建IVs
*eIV=(生成([]字节,4))
n1,u:=随机读数(*eIV)
*dIV=(make([]字节,4))
n2,u:=随机读数(*dIV)
如果(n1+n2<8){
fmt.Println(“IV生成错误”)
}    
//创建数据包
数据包添加(版本)
packet.AddString(位置)
packet.AddByte(*dIV)
packet.AddByte(*eIV)
packet.AddByte(区域设置)
fmt.Println(packet.GetBytes())
返回数据包。GetBytes()
}

但是,当创建上面示例中的数据包并添加值时,packet.GetBytes()返回一个空数组。是字节。缓冲区正确的方法是什么?还是我在处理这个问题时完全错了?

Go按值传递所有参数,包括接收者

尝试使用指针接收器:
(p*数据包)
<代码>字节。缓冲区包含正在丢弃的状态信息



围棋编程语言

方法

关于指针与接收器值的规则是,值方法 可以对指针和值调用,但指针方法只能 在指针上调用。这是因为指针方法可以修改 接受者在值的副本上调用它们会导致 要放弃的修改

您的类型
程序包
类型等同于以下内容

type Packet struct {
    buffer /* bytes.Buffer */ struct {
    buf       []byte            // contents are the bytes buf[off : len(buf)]
    off       int               // read at &buf[off], write at &buf[len(buf)]
    runeBytes [utf8.UTFMax]byte // avoid allocation of slice on each WriteByte or Rune
    bootstrap [64]byte          // memory to hold first slice; helps small buffers (Printf) avoid allocation.
    lastRead  readOp            // last read operation, so that Unread* can work correctly.
}

类型变量的副本(按值)传递给方法。副本将更新以反映新状态,并在返回时丢弃

我终于明白你的意思了!谢谢你的帮助。
type Packet struct {
    buffer /* bytes.Buffer */ struct {
    buf       []byte            // contents are the bytes buf[off : len(buf)]
    off       int               // read at &buf[off], write at &buf[len(buf)]
    runeBytes [utf8.UTFMax]byte // avoid allocation of slice on each WriteByte or Rune
    bootstrap [64]byte          // memory to hold first slice; helps small buffers (Printf) avoid allocation.
    lastRead  readOp            // last read operation, so that Unread* can work correctly.
}