首頁  >  文章  >  後端開發  >  建立試用或商業二進位檔案並相應地更改運行時行為

建立試用或商業二進位檔案並相應地更改運行時行為

王林
王林轉載
2024-02-09 13:39:08927瀏覽

建立試用或商業二進位檔案並相應地更改運行時行為

php小編新教你如何建立試用或商業二進位文件,並相應地更改運行時行為。無論是為了保護軟體版權,還是為了提供試用版,建立二進位檔案是一個重要的步驟。透過更改運行時行為,可以實現一些功能的限製或控制。本文將介紹建置二進位檔案的基本步驟,並給出一些常見的執行時間行為修改範例。跟著本文,你將掌握建立二進位檔案的技巧,為你的軟體產品增添更多的價值和保護。

問題內容

建構

我有這個 Makefile 來建立一個二進位檔案供試用商業使用:

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

原始碼

在我的原始程式碼中,我有一個斷言試用商業限制的原始程式碼:

<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>

我計劃在運行時呼叫上述兩個函數,如下所示:

<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>

問題

在運行時檢測二進位檔案是建構為試用還是商業的最佳實踐是什麼?我覺得我不知道可用的標準工具來做到這一點。

解決方法

最終,我繼續使用 Go 建立標籤方法:

permit\build_Trial.go:

<code>//go:build trial

package permit

var buildType = "trial"
</code>

permit\build_commercial.go:

<code>//go:build commercial

package permit

var buildType = "commercial"
</code>

然後,我可以有這樣的語句:

<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>

最終的Makefile為:

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

以上是建立試用或商業二進位檔案並相應地更改運行時行為的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除