Go language operation binary file

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Contents of binary file:

Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F00000000 12 31 23 12 31 22 31 23 12 31 22 22 22 22 22 22 # 1 "1" "" "" "00000010 22 22 22 12 23 12 23 00 00 00 00 00 00 00 00 00" "" # # 00000020 00 00 00 00 00 00                   00 00 00 00 00 00 00 00 00 00 00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00000 070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00000080 00 00 00 00 00 00 00 00 00 00 00 00 0 0 xx, 00000090, xx, xx, xx, 000000a0 00 0                    0 xx, xx, xx, xx 000000b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 000000C0, XX, xx, xx, 000000d0 00 00 00 00 00 00 00 00 00 00 00 00 0                    0 00 00 00

Code:

Package Mainimport ("bytes" "FMT" "IO" "OS") Func main () {file, err: = OS. OpenFile ("Test", OS. O_rdonly, OS. MODEPERM) If err! = Nil {defer file. Close () OS. Exit (0)}file. Seek (8, 0) fmt. Println ("Success Open File") var buffer bytes. Bufferio.copyn (&buffer, file, 8) _bytes: = buffer. Bytes () var magic []byte = []byte{0x12, 0x31, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22}for _, Byte: = Range _bytes {fmt. Printf ("%02x", "byte")}fmt. Println () If Bytes.compare (Magic, _bytes) = = 0 {fmt. Println ("Equal")}}

Results:

Success Open File12-Equal


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.