将Go(golang)文件分别编译,并一起使用。

huangapple go评论86阅读模式
英文:

Compile go (golang) files seperately and use together

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我是Go语言的新手,之前学过Python。

我的问题是,我想编写一个程序,在主程序中根据某个条件在运行时包含一个Go程序,否则独立运行;如果条件成立,就包含并运行那个程序。我之前在Python中可以给你一个提示:

#一些代码

if userQuery=="ip":

from . import ipInfo

ip = input("输入IP地址: ")

print(ipInfo(ip))

elif userQuery==其他条件:

做其他事情()

#一些代码

我的程序很大,所以我希望它能在Go中在运行时被包含进来。

我正在尝试在运行时导入和运行Go程序。

英文:

i am new in golang, and learnt python;

My question is,
I want to write a program which includes a go program in runtime if any condition is satisfied in the main program, otherwise it will not include it and run independentaly; if the condition become true, it will include that and run that program. I am come from python and can give you hint in python:-

#SOME CODES

if userQuery=="ip":

from . import ipInfo

ip = input("Enter ip: ")

print(ipInfo(ip))

elif userQuery==anything:

doanything()

#SOME CODES

My program is large so I want that it will include in runtime in go.

I am trying to import and run go program at runtime.

答案1

得分: 1

Go是一种编译语言,所以你不能像脚本语言那样进行动态导入。正如评论中提到的,你可以使用plugin来实现类似的功能,但在这种情况下,你仍然需要一个编译版本的代码,以便根据条件进行导入。插件的概念,顾名思义,通过发布新的插件来帮助你向程序中添加功能。另外,请注意插件在Windows上不可用。

英文:

Go is a compiled language, so you cannot do things like dynamic import as you can do in scripting language. As mentioned in the comments you can do similar with plugin, but in that case even you need a compiled version of your code, so you can import it based on your conditions. Plugin concepts, as it name suggests, helping you to add features into your program by releasing new add-ons. Also, note that plugins are not available on Windows.

huangapple
  • 本文由 发表于 2022年11月12日 06:05:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/74408593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定