gpt4 book ai didi

Golang 二进制文件保留源代码名称和路径的引用

转载 作者:数据小太阳 更新时间:2023-10-29 03:38:32 26 4
gpt4 key购买 nike

出于好奇,我使用 HEX 编辑器打开了由 GO 编译器生成的二进制文件,令我惊讶的是,我在二进制文件中找到了我项目中使用的每个源代码的完整路径和文件名。

像,明明有/home/myusername/myprojectname/source.go这样的信息

更惊讶的是,我发现了一些关于特定功能如何工作的注释之类的东西

enter image description here

现在我知道为什么用 Golang 编写并编译的“Hello World”会生成一个 3mb 的二进制文件...

有谁知道是否有办法从 golang 二进制文件中删除这些东西?比如,任何编译器标志?

最佳答案

I found things like comments of how specific function works


它们不是评论。它们是字符串文字,主要是错误消息。例如,

malloc.go:  throw("persistentalloc: align is not a power of 2")

lfstack.go: print("runtime: lfstack.push invalid packing: node="

Now I know why a "Hello World" written in Golang and compiled generates a 3mb binary...

不,你不知道。

Go: Frequently Asked Questions (FAQ)

Why is my trivial program such a large binary?

The linker in the gc toolchain creates statically-linked binaries by default. All Go binaries therefore include the Go runtime, along with the run-time type information necessary to support dynamic type checks, reflection, and even panic-time stack traces.

A simple C "hello, world" program compiled and linked statically using gcc on Linux is around 750 kB, including an implementation of printf. An equivalent Go program using fmt.Printf weighs a couple of megabytes, but that includes more powerful run-time support and type and debugging information.

A Go program compiled with gc can be linked with the -ldflags=-w flag to disable DWARF generation, removing debugging information from the binary but with no other loss of functionality. This can reduce the binary size substantially.


a "Hello World" written in Golang and compiled generates a 3mb binary...

这有点夸张;它是 2.0MB。

$ go version
go version devel +c7026f9d14 Sat Feb 9 17:34:48 2019 +0000 linux/amd64
$ cat hello.go
package main

import "fmt"

func main() {
fmt.Println("Hello, 世界")
}
$ go build hello.go
$ ./hello
Hello, 世界
$ ls -s -h ./hello
2.0M ./hello
$

关于Golang 二进制文件保留源代码名称和路径的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54648916/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com