简体   繁体   中英

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

root
    -> main2.go
    -> main1.go

the goal is to run Makefile, which will create 2 different executables. 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. Create a package for each executable.

How it is can be done?

By using different cmd packages inside your project, as I mentioned before .

This is described in details with " Packing multiple binaries in a Golang package " in 2018, by Ilija Eftimov

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

A go install./... would find those main packages and compile/install them separately.


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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