Home > Backend Development > Golang > How to use binaries in Go?

How to use binaries in Go?

WBOY
Release: 2023-05-11 15:51:36
Original
2773 people have browsed it

Go language is a strongly typed, statically typed, compiled language. It is widely used in network programming, server development, system programming and other fields. Using binary files in Go language is a very common task. This article will detail how to use binary files in Go.

1. What is a binary file?

In a computer, a binary file is a file that the machine can directly execute. It contains all program code and data needed at run time. Binaries are typically produced by a compiler's process of compiling source code into machine executable code. In Unix/Linux systems, the suffix of binary files is usually ".bin" or ".elf".

There are two main types of binary files involved in the Go language: executable files and library files. An executable file is a binary file that can be directly executed. Common executable files include the executable file of the Go program (suffix ".exe" or no suffix), the executable file of the C program (suffix ".out"), etc. wait. A library file is a binary file containing functions and data that can be linked to other programs. The suffix of library files is usually ".a" (static library) or ".so" (dynamic library).

2. Generate executable file

  1. Write source code

First, we need to write Go language source code, such as the following simple program, It outputs "Hello, world!":

package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}
Copy after login

Save the above code to the file "main.go".

  1. Compile the executable file

Use the Go language compiler to compile the source code and generate the executable file. Execute the following command:

go build main.go
Copy after login
Copy after login

After execution, an executable file named "main" will be generated. We can run this file in the command line window:

./main
Copy after login

After running, "Hello, world!" will be output to the terminal.

  1. Cross-compilation

If we need to run our program on different operating systems or architectures, we can use cross-compilation to generate executable files on different platforms . For example, we can compile a binary file on Mac that can run on Linux:

GOOS=linux GOARCH=amd64 go build main.go
Copy after login

After execution, an executable file named "main" will be generated in the current directory, which can run on Linux .

3. Generate library files

  1. Write source code

First, we need to write Go language source code, such as the following simple library file, It defines a function Add that adds two integers and returns:

package mylib

func Add(a, b int) int {
    return a + b
}
Copy after login

Save the above code to the file "mylib.go".

  1. Compile library file

Use the Go language compiler to compile the source code and generate the library file. Execute the following command:

go build -o mylib.a -buildmode=c-archive mylib.go
Copy after login

After execution, a static library file named "mylib.a" will be generated in the current directory.

  1. Using library files

It is very easy to use library files in Go language. You only need to import the library file in the source code file that needs to use the library function. For example, in the following program, we import the "mylib" library file and call the Add function in it:

package main

import (
    "fmt"
    "mylib"
)

func main() {
    sum := mylib.Add(1, 2)
    fmt.Println(sum)
}
Copy after login

Execute the following command to compile the program:

go build main.go
Copy after login
Copy after login

After execution, Output "3" to the terminal.

4. Reading binary files

Go language provides some powerful APIs for reading binary files, such as os.Open function, io.ReadFull function, etc. These APIs allow us to read data from binary files and convert them into variables in Go language.

The following is an example, which opens a binary file named "test.bin" and reads an integer in it:

package main

import (
    "encoding/binary"
    "fmt"
    "os"
)

func main() {
    // 打开二进制文件
    file, err := os.Open("test.bin")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer file.Close()

    // 读取一个int32类型的整数
    var num int32
    err = binary.Read(file, binary.LittleEndian, &num)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println("Num is", num)
}
Copy after login

Execute the above code, it will read an integer Be an integer in the binary file "test.bin" and print it to the terminal.

5. Summary

Using binary files is one of the basic tasks in the Go language. In this article, we covered how to generate executables and libraries in Go, and use libraries. At the same time, we also learned how to read data from binary files. These skills are all necessary to write truly useful applications. We can further extend and customize these features through advanced concepts of the Go language such as addresses and pipes. Therefore, it is very useful to master the skills of working with binaries in Go language.

The above is the detailed content of How to use binaries in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template