Home >Backend Development >Golang >Build trial or commercial binaries and change runtime behavior accordingly
#php editor teaches you how to build trial or commercial binaries and change the runtime behavior accordingly. Whether to protect software copyright or to provide a trial version, building binaries is an important step. By changing the runtime behavior, some functional limitations or controls can be achieved. This article walks through the basic steps of building a binary and gives some examples of common runtime behavior modifications. Following this article, you will master the techniques of building binaries to add more value and protection to your software products.
I have this Makefile
to build a binary for trial and commercial use:
BINARY_NAME=App.exe trial: ENV_BUILD=trial go build -o ${BINARY_NAME} comme: ENV_BUILD=comme go build -o ${BINARY_NAME} clean: go clean rm ${BINARY_NAME} prepare: go mod tidy
In my source code I have an assertion Trial and Commercial restricted source code:
<code>package permit import "fmt" func Trial() (bool, error) { fmt.Println("You are using a limited trial release.") // ... // Assert limitations... } func Comme() (bool, error) { fmt.Println("You are using the unlimited commercial release.") // ... } </code>
I plan to call the above two functions at runtime as follows:
<code>package main import "permit" // ... var builtTrial bool // TODO: Best way to detect if the built has been trial var builtComme bool // TODO: Best way to detect if the built has been commercial if builtTrial { permitted, err := permit.Trial() } else if builtComme { permitted, err := permit.Comme() } // ... </code>
What is the best practice for detecting at runtime whether a binary is built as Trial or Commercial? I feel like I'm not aware of the standard tools available to do this.
Eventually, I continued to use Go to build the tag method:
File permit\build_Trial.go
:
<code>//go:build trial package permit var buildType = "trial" </code>
File permit\build_commercial.go
:
<code>//go:build commercial package permit var buildType = "commercial" </code>
Then, I can have statements like this:
<code>package permit // ... if buildType == "trial" { fmt.Println("You are using a limited trial release.") return nil } else if buildType == "commercial" { fmt.Println("You are using the unlimited commercial release.") return nil } else { return fmt.Errorf("build type is neither trial nor commercial.") } </code>
The final Makefile
is:
BINARY_NAME=App.exe trial: go build -tags trial -o ${BINARY_NAME} commercial: go build -tags commercial -o ${BINARY_NAME} clean: go clean rm ${BINARY_NAME} prepare: go mod tidy
The above is the detailed content of Build trial or commercial binaries and change runtime behavior accordingly. For more information, please follow other related articles on the PHP Chinese website!