找不到Marshal()函数。

huangapple go评论95阅读模式
英文:

Not able to find function Marshal()

问题

我正在为生成ICMP数据包编写一小段代码。为此,我正在参考golang net包中的代码。

我找到了以下用于生成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包文档中没有包含,请告诉我如何找到这个函数,以及为什么它没有包含在net包中。

  2. 请解释一下第121行的代码(icmpMessage结构体和函数调用),因为这些在文件中没有定义。

英文:

I am writing small code for generating the ICMP packet. For that I am following the code from golang net package.

I identified the following code to use for ICMP packet generation:

   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			}

I am not able to understand followings in this code :

  1. Marshal() is not included in the net package documentation, please let me know how to find this function and why it is not included in the net package.

  2. please explain me line #121 (icmpMessage struct and function calling as these are not defined in this file).

答案1

得分: 0

  1. Marshal() 在这里定义:https://golang.org/src/net/mockicmp_test.go?h=Marshal#L32
  2. icmpMessage 结构体在这里定义:https://golang.org/src/net/mockicmp_test.go?h=icmpMessage#L17
英文:
  1. Marshal() defined here https://golang.org/src/net/mockicmp_test.go?h=Marshal#L32
  2. And icmpMessage struct here https://golang.org/src/net/mockicmp_test.go?h=icmpMessage#L17

huangapple
  • 本文由 发表于 2014年12月29日 17:31:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/27686338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定