Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

golang如何判断是从源码运行还是从二进制文件运行?

开发和测试的时候需要加载配置文件,但是由于golang的配置文件用的是相对路径,main.go是在cmd下面的,这和二进制文件获得base dir是不同的,如何判断是go run main.go运行还是./main运行


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

linux下,二进制启动的ppid会等于sid。也查找父进程的启动参数可以看出。

关于gobuild和gorun启动的dir不同,可以在配置设置一个workdir,在启动的时候自动os.Chdir()切换到workdir去。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...