我知道将高级语言代码转换为机器语言或可执行代码需要三个步骤,即编译、汇编和链接。
按照go文档go tool编译执行以下操作- It then writes a single object file named for the basename of the first source file with a .o suffix
因此,最终的目标文件必须包含每个文件的机器语言代码(在编译和汇编后运行)。如果我在go文件上通过go tool compile -S,它会显示go生成的汇编语言。
之后,当我在目标文件上运行go tool link时,它必须链接所有需要的目标文件(如果有多个),然后生成
我在竞速检测器中对println和fmt.Println有不同的输出,我无法解释。我希望两者都是种族,或者至少两者都不是种族。
package main
var a int
func f() {
a = 1
}
func main() {
go f()
println(a)
}
并且,它发现了预期的竞争条件。
0
==================
WARNING: DATA RACE
Write by goroutine 5:
main.f()
/home/felmas/test.go:6 +0x30
Previous read by main goro
我理解Go中的程序是从main函数开始运行的。但是我想知道为一个新包创建函数的工作流是什么。
例如,在python中,当模块被直接调用时,我在模块中使用__main__。当从另一个文件导入模块时,__main__将被忽略。这在开发新模块时很有用。
if __name__ == "__main__":
# Run module code here if module called directly
对于Go,我使用一个test.go文件和一个package main以及我的main.go文件来测试我正在创建的包中的函数。
// test.go
package main
下面是代码:
class Program
{
public static void Main()
{
var test = new Test();
test.Go(1);
test.Go(100);
test.Go(10000);
test.Go(1.0);
test.Go(100.0);
test.Go(10000.0);
test.Go(65535.0);
test.Go(1000000000);
te
如果我运行go build -o bin/test ./scripts/test.go,我得到一个可执行文件,但当我尝试运行它时,我得到
Failed to execute process './bin/test'. Reason:
exec: Exec format error
The file './bin/test' is marked as an executable but could not be run by the operating system.
scripts/test.go
package scripts
import (
我想从另一个go程序内部执行另一个go程序,并给它提供args,如下所示:
package main
func main() {
//here I want to call a specific go file, e.g. test.go with args
}
test.go
package main
func main(x int) {
//do stuff with x
}
我不想把int作为arg发送,但是像http.ResponseWriter这样的东西
我想出了一个解决办法,但不是很好:
使用gob将http.ResponseWriter转换为字符串
从test.g
在Go中,文件名具有语义含义。例如:
*_windows.go // Only include in compilations for Windows
*_unix.go // Only include in compilations for Unix
*_386.go // Only include in compilations for 386 systems
*_test.go // Only include when run with `go test`
然而,我似乎不能让以下内容工作:
*_windows_test.go // Only include when
我正在尝试将一个C++库导入到Go应用程序中。
据说Go可以链接到C++文件..。或者至少Go是这么说的(我用的是Go 1.3)。我不认为它是C++,但我不太了解C++,所以我不确定发生了什么。
它似乎在说它不承认<string>是一个C++包含。
它给出的编译错误是:
# go build test.go
# command-line-arguments
In file included from api-main-binarize.cc:14:0,
from ./test.go:4:
doc-binarize.h:15:19: fatal er
下面是使用atomic.Value测试结构B的写和读交互访问的代码,但是我发现了一些错误,指示无效的指针访问。那我该怎么办?做这件事的习惯是什么?
type A struct {
numMap map[string]int
}
type B struct {
numMap map[string]*A
}
var store atomic.Value
var chanA chan bool = make(chan bool, 100)
var chanB chan bool = make(chan bool, 100)
var b *B = &B{}
func