英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论