Home  >  Article  >  Backend Development  >  How to manipulate binary files in Golang?

How to manipulate binary files in Golang?

王林
王林Original
2024-03-21 10:18:03708browse

How to manipulate binary files in Golang?

How to operate binary files in Golang?

In Golang, operating binary files is a common task that can be achieved through some functions provided by the standard library. This article will introduce how to read, write and edit binary files in Golang, with specific code examples.

1. Create a binary file

First, we need to import the relevant package:

package main

import (
    "os"
    "encoding/binary"
)

We can then create a binary file and write some data with the following code example:

func main() {
    file, err := os.Create("binaryfile.bin")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    data := []int{1, 2, 3, 4, 5}

    for _, num := range data {
        binary.Write(file, binary.LittleEndian, num)
    }

    println("Binary file has been created and data written")
}

In the above code, we first created a binary file named binaryfile.bin, and put the data in the integer array data in little endian order. way to write to the file.

2. Read binary file

Next, let’s see how to read the data in the binary file we just created:

func main() {
    file, err := os.Open("binaryfile.bin")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    var num int
    for {
        err := binary.Read(file, binary.LittleEndian, &num)
        if err != nil {
            break
        }
        println(num)
    }

    println("Binary file has been read")
}

In the above code, we first opened the previously created binary file binaryfile.bin, and then used the binary.Read function to read the integer data one by one, and print it out.

3. Edit binary files

If we want to edit an existing binary file, we can do so through the following code example:

func main() {
    file, err := os.OpenFile("binaryfile.bin", os.O_RDWR, 0666)
    if err != nil {
        panic(err)
    }
    defer file.Close()

    var newData = []int{6, 7, 8}

    for _, num := range newData {
        binary.Write(file, binary.LittleEndian, num)
    }

    println("Binary file has been edited")
}

In the above code, we first opened the previous binary file binaryfile.bin in read-write mode, and then added the new integer data array newData to It is written to the file in endian order and realizes the editing operation of the binary file.

Through the above examples, we have introduced in detail how to operate binary files in Golang, including creating, reading and editing binary files, and attached corresponding code examples. We hope it will be helpful to you.

The above is the detailed content of How to manipulate binary files in Golang?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn