简体   繁体   English

GO | 在同一模块上创建 2 个不同的可执行文件

[英]GO | create 2 different executables on the same module

I would like to know if it is possible to create 2 different executables on the same module, main1.go -> main1.exe main2.go -> main2.exe我想知道是否可以在同一个模块上创建 2 个不同的可执行文件,main1.go -> main1.exe main2.go -> main2.exe

root
    -> main2.go
    -> main1.go

the goal is to run Makefile, which will create 2 different executables.目标是运行 Makefile,这将创建 2 个不同的可执行文件。 Thanks谢谢

LATE_TARGET_HOOK=make_main
LATE_TARGET_HOOK=make_client
DS_CONF = ds.conf

export GOROOT := $(UV_golang_PKG)
export GOPROXY := http://****-product.****.com:****/artifactory/api/go/go
export GOSUMDB := off

export PATH := $(PATH):$(GOROOT)/bin
export VERSION := $(shell (cat $(SRCROOT)/VERSION))
GO =$(GOROOT)/bin/go

CONF_FILES = VERSION

include $(MODULEMK)

ifndef UV_BUILDNUMBER
    UV_BUILDNUMBER = 0000
endif

make_main:
    ${GO} mod tidy
    GOARCH=amd64 GOOS=linux CGO_CFLAGS=$(CGO_CFLAGS) CGO_LDFLAGS=$(CGO_LDFLAGS) $(GO) build $(BUILD_FLAGS) -o $(SRCROOT)/CMpub/bin/$(UV_O)/agentExporters .

make_client:
    ${GO} mod tidy
    GOARCH=amd64 GOOS=linux CGO_CFLAGS=$(CGO_CFLAGS) CGO_LDFLAGS=$(CGO_LDFLAGS) $(GO) build $(BUILD_FLAGS) -o $(SRCROOT)/CMpub/bin/$(UV_O)/disableCollector .

but a single package cannot.但单个 package 不能。 Create a package for each executable.为每个可执行文件创建一个 package。

How it is can be done?它是如何做到的?

By using different cmd packages inside your project, as I mentioned before .正如我之前提到的,通过在项目中使用不同的 cmd 包。

This is described in details with " Packing multiple binaries in a Golang package " in 2018, by Ilija Eftimov Ilija Eftimov在 2018 年的“ Packing multiple binaries in a Golang package ”中对此进行了详细描述

› tree
.
└── cmd
    ├── cookie
    │   └── main.go
    └── eight_ball
        └── main.go

A go install./... would find those main packages and compile/install them separately. A go install./...会找到那些主要包并单独编译/安装它们。


A more modern/different approach would be with " Getting started with multi-module workspaces ": you could work on different modules (one per command) within the same workspace.一种更现代/不同的方法是“开始使用多模块工作区”:您可以在同一工作区内处理不同的模块(每个命令一个)。

With multi-module workspaces, you can tell the Go command that you're writing code in multiple modules at the same time and easily build and run code in those modules.使用多模块工作区,您可以告诉 Go 命令您正在同时在多个模块中编写代码,并轻松地在这些模块中构建和运行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM