go - 找不到函数 Marshal()

标签 go package icmp

我正在编写用于生成 ICMP 数据包的小代码。为此,我遵循 golang net package 中的代码.

我确定了以下用于生成 ICMP 数据包的代码:

   109          c, err := Dial(tt.net, tt.raddr)
   110          if err != nil {
   111              t.Fatalf("Dial failed: %v", err)
   112          }
   113          c.SetDeadline(time.Now().Add(100 * time.Millisecond))
   114          defer c.Close()
   115  
   116          typ := icmpv4EchoRequest
   117          if net == "ip6" {
   118              typ = icmpv6EchoRequest
   119          }
   120          xid, xseq := os.Getpid()&0xffff, i+1
   121          wb, err := (&icmpMessage{
   122              Type: typ, Code: 0,
   123              Body: &icmpEcho{
   124                  ID: xid, Seq: xseq,
   125                  Data: bytes.Repeat([]byte("Go Go Gadget Ping!!!"), 3),
   126              },
   127          }).Marshal()
   128          if err != nil {
   129              t.Fatalf("icmpMessage.Marshal failed: %v", err)
   130          }
   131          if _, err := c.Write(wb); err != nil {
   132              t.Fatalf("Conn.Write failed: %v", err)
   133          }

我无法理解这段代码中的以下内容:

  1. Marshal() 不包含在 net package documentation 中,请让我知道如何找到这个函数以及为什么它不包含在 net 包中。

  2. 请向我解释第 121 行(icmpMessage 结构和函数调用,因为这些未在此文件中定义)。

最佳答案

关于go - 找不到函数 Marshal(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27686338/

相关文章:

google-app-engine - 清理 AppEngine BlobStore

json - Go JSON命名策略

python - 清理requirements.txt

linux - wireshark 不显示两个逻辑接口(interface)之间的 icmp 流量

c - C 中简单的 icmp traceroute 实现

xml - 在Golang中使用encoding/xml包制作soap xml

google-app-engine - Google App Engine Golang 没有这样的文件或目录

android - 从另一个包开始 Activity

R:如何判断任务 View 中的哪些包具有关联的小插图或演示

python - 在 ipv6 的 sendto 中作为 "address"传递的到底是什么