英文:
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 }
我无法理解代码中的以下部分:
-
Marshal()
函数在net包文档中没有包含,请告诉我如何找到这个函数,以及为什么它没有包含在net包中。 -
请解释一下第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 :
-
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. -
please explain me line #121 (
icmpMessage
struct and function calling as these are not defined in this file).
答案1
得分: 0
- Marshal() 在这里定义:https://golang.org/src/net/mockicmp_test.go?h=Marshal#L32
- icmpMessage 结构体在这里定义:https://golang.org/src/net/mockicmp_test.go?h=icmpMessage#L17
英文:
- Marshal() defined here https://golang.org/src/net/mockicmp_test.go?h=Marshal#L32
- And icmpMessage struct here https://golang.org/src/net/mockicmp_test.go?h=icmpMessage#L17
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论