This is a creation in Article, where the information may have evolved or changed.
Read the header of a tcpdump file and print it out. The OS is used. Open (), OS. Close () to open the closed file. Use Bufio.reader.read () to read the data to a byte array.
The go language doesn't seem to be automatically transformed, so a byte shift is forced before it shifts.
PackageMainImport("FMT" "OS" "Bufio")funcParsetcpdumpheader (h[]byte){varMagicUInt32=UInt32(h[3]) <<24|UInt32(h[2]) <<16|UInt32(h[1]) <<8|UInt32(h[0]);varMajoruint16=uint16(h[5]) <<8|uint16(h[4]);varMinoruint16=uint16(h[7]) <<24|uint16(h[6]); Fmt. Printf ("magic:0x%x, Major:%x, minor:%x\r\n", Magic, Major, minor);}funcMain () {FMT. Printf ("Hello,%s!\r\n.","World")varF, err = OS. Open ("./1.cap")deferF.close ();if(err!=Nil) {FMT. Printf ("Open file failed.\r\n"); }//var tcpdumpheader = [4]byte; varReader = Bufio. Newreader (f)varTcpdumpheader = Make([]byte, -); Fmt. Printf ("Len:%d\r\n",Len(Tcpdumpheader));varN, err2 = reader. Read (Tcpdumpheader)if(err2!=Nil) {FMT. Printf ("Read file error.\r\n"); }if(n==Len(Tcpdumpheader)) {Parsetcpdumpheader (Tcpdumpheader)}}