搜尋
首頁後端開發Golang初學者 Go 專案 - 在 Go 中建立任務運行程序

Beginner Go Project - Create a Task Runner in Go

我們要建造什麼

我們將製作一個像 make 這樣的工具,我們可以使用像這樣的簡單 yaml 檔案來運行任務。

tasks:
    build:
        description:  "compile the project"
        command:  "go build main.go"
        dependencies:  [test]
    test:
        description:  "run unit tests"
        command:  "go test -v ./..."

讓我們開始吧,首先我們需要概述行動過程。我們已經定義了任務文件架構。我們可以使用 json 來代替 yaml,但為了這個項目,我們將使用 yml 檔。

從檔案中我們可以看到,我們需要一個結構來儲存單一任務,以及在繼續主任務之前運行依賴任務的方法。讓我們從啟動我們的專案開始。建立一個新資料夾並運行:

go mod init github.com/vishaaxl/mommy

您可以隨意命名您的項目,我將使用「媽媽」的名字。我們還需要安裝一些套件來處理 yaml 檔案 - 基本上將它們轉換為地圖物件。繼續安裝以下軟體包。

go get gopkg.in/yaml.v3

接下來建立一個新的 main.go 檔案並從定義「Task」結構開始。

package main

import (
    "gopkg.in/yaml.v3"
)
// Task defines the structure of a task in the configuration file.
// Each task has a description, a command to run, and a list of dependencies
// (other tasks that need to be completed before this task).
type Task struct {
    Description  string   `yaml:"description"`  // A brief description of the task.
    Command      string   `yaml:"command"`      // The shell command to execute for the task.
    Dependencies []string `yaml:"dependencies"` // List of tasks that need to be completed before this task.
}

這個是非常不言自明的。這將保存每個單獨任務的價值。接下來,我們還需要一個結構體來儲存任務清單並將 .yaml 檔案的內容載入到這個新物件中。

// Config represents the entire configuration file,
// which contains a map of tasks by name.
type Config struct {
    Tasks map[string]Task `yaml:"tasks"` // A map of task names to task details.
}

// loadConfig reads and parses the configuration file (e.g., Makefile.yaml),
// and returns a Config struct containing the tasks and their details.
func loadConfig(filename string) (Config, error) {
    // Read the content of the config file.
    data, err := os.ReadFile(filename)
    if err != nil {
        return Config{}, err
    }

    // Unmarshal the YAML data into a Config struct.
    var config Config
    err = yaml.Unmarshal(data, &config)
    if err != nil {
        return Config{}, err
    }

    return config, nil
}

接下來我們需要建立一個執行單一任務的函數。我們將使用 os/exec 模組在 shell 中執行任務。 在 Golang 中,os/exec 套件提供了一種執行 shell 指令和外部程式的方法。

// executeTask recursively executes the specified task and its dependencies.
// It first ensures that all dependencies are executed before running the current task's command.
func executeTask(taskName string, tasks map[string]Task, executed map[string]bool) error {
    // If the task has already been executed, skip it.
    if executed[taskName] {
        return nil
    }

    // Get the task details from the tasks map.
    task, exists := tasks[taskName]
    if !exists {
        return fmt.Errorf("task %s not found", taskName)
    }

    // First, execute all the dependencies of this task.
    for _, dep := range task.Dependencies {
        // Recursively execute each dependency.
        if err := executeTask(dep, tasks, executed); err != nil {
            return err
        }
    }

    // Now that dependencies are executed, run the task's command.
    fmt.Printf("Running task: %s\n", taskName)
    fmt.Printf("Command: %s\n", task.Command)

    // Execute the task's command using the shell (sh -c allows for complex shell commands).
    cmd := exec.Command("sh", "-c", task.Command)
    cmd.Stdout = os.Stdout // Direct standard output to the terminal.
    cmd.Stderr = os.Stderr // Direct error output to the terminal.

    // Run the command and check for any errors.
    if err := cmd.Run(); err != nil {
        return fmt.Errorf("failed to execute command %s: %v", task.Command, err)
    }

    // Mark the task as executed.
    executed[taskName] = true
    return nil
}

現在我們擁有了程式的所有建構塊,我們可以在主函數中使用它們來載入設定檔並開始自動化。我們將使用 flag 套件來讀取命令列標誌。

func main() {
    // Define command-line flags
    configFile := flag.String("f", "Mommy.yaml", "Path to the configuration file") // Path to the config file (defaults to Makefile.yaml)
    taskName := flag.String("task", "", "Task to execute")                             // The task to execute (required flag)

    // Parse the flags
    flag.Parse()

    // Check if the task flag is provided
    if *taskName == "" {
        fmt.Println("Error: Please specify a task using -task flag.")
        os.Exit(1) // Exit if no task is provided
    }

    // Load the configuration file
    config, err := loadConfig(*configFile)
    if err != nil {
        fmt.Printf("Failed to load config: %v\n", err)
        os.Exit(1) // Exit if the configuration file can't be loaded
    }

    // Map to track which tasks have been executed already (avoiding re-execution).
    executed := make(map[string]bool)

    // Start executing the specified task (with dependencies)
    if err := executeTask(*taskName, config.Tasks, executed); err != nil {
        fmt.Printf("Error executing task: %v\n", err)
        os.Exit(1) // Exit if task execution fails
    }
}

讓我們測試一下整個過程,建立一個新的 Mommy.yaml 並將開頭的 yaml 程式碼貼到其中。我們將使用任務運行程序為我們的專案建立二進位檔案。運行:

go run main.go -task build

如果一切順利,您將在資料夾的根目錄中看到一個新的 .exe 檔案。太好了,我們現在有了一個可以工作的任務運行程序。我們可以在系統的環境變數中新增此 .exe 檔案的位置,並使用以下命令從任何地方使用它:

 mommy -task build

完整程式碼

package main

import (
    "flag"
    "fmt"
    "os"
    "os/exec"
    "gopkg.in/yaml.v3"
)

// Task defines the structure of a task in the configuration file.
// Each task has a description, a command to run, and a list of dependencies
// (other tasks that need to be completed before this task).
type Task struct {
    Description  string   `yaml:"description"`  // A brief description of the task.
    Command      string   `yaml:"command"`      // The shell command to execute for the task.
    Dependencies []string `yaml:"dependencies"` // List of tasks that need to be completed before this task.
}

// Config represents the entire configuration file,
// which contains a map of tasks by name.
type Config struct {
    Tasks map[string]Task `yaml:"tasks"` // A map of task names to task details.
}

// loadConfig reads and parses the configuration file (e.g., Makefile.yaml),
// and returns a Config struct containing the tasks and their details.
func loadConfig(filename string) (Config, error) {
    // Read the content of the config file.
    data, err := os.ReadFile(filename)
    if err != nil {
        return Config{}, err
    }

    // Unmarshal the YAML data into a Config struct.
    var config Config
    err = yaml.Unmarshal(data, &config)
    if err != nil {
        return Config{}, err
    }

    return config, nil
}

// executeTask recursively executes the specified task and its dependencies.
// It first ensures that all dependencies are executed before running the current task's command.
func executeTask(taskName string, tasks map[string]Task, executed map[string]bool) error {
    // If the task has already been executed, skip it.
    if executed[taskName] {
        return nil
    }

    // Get the task details from the tasks map.
    task, exists := tasks[taskName]
    if !exists {
        return fmt.Errorf("task %s not found", taskName)
    }

    // First, execute all the dependencies of this task.
    for _, dep := range task.Dependencies {
        // Recursively execute each dependency.
        if err := executeTask(dep, tasks, executed); err != nil {
            return err
        }
    }

    // Now that dependencies are executed, run the task's command.
    fmt.Printf("Running task: %s\n", taskName)
    fmt.Printf("Command: %s\n", task.Command)

    // Execute the task's command using the shell (sh -c allows for complex shell commands).
    cmd := exec.Command("sh", "-c", task.Command)
    cmd.Stdout = os.Stdout // Direct standard output to the terminal.
    cmd.Stderr = os.Stderr // Direct error output to the terminal.

    // Run the command and check for any errors.
    if err := cmd.Run(); err != nil {
        return fmt.Errorf("failed to execute command %s: %v", task.Command, err)
    }

    // Mark the task as executed.
    executed[taskName] = true
    return nil
}

func main() {
    // Define command-line flags
    configFile := flag.String("f", "Makefile.yaml", "Path to the configuration file") // Path to the config file (defaults to Makefile.yaml)
    taskName := flag.String("task", "", "Task to execute")                             // The task to execute (required flag)

    // Parse the flags
    flag.Parse()

    // Check if the task flag is provided
    if *taskName == "" {
        fmt.Println("Error: Please specify a task using -task flag.")
        os.Exit(1) // Exit if no task is provided
    }

    // Load the configuration file
    config, err := loadConfig(*configFile)
    if err != nil {
        fmt.Printf("Failed to load config: %v\n", err)
        os.Exit(1) // Exit if the configuration file can't be loaded
    }

    // Map to track which tasks have been executed already (avoiding re-execution).
    executed := make(map[string]bool)

    // Start executing the specified task (with dependencies)
    if err := executeTask(*taskName, config.Tasks, executed); err != nil {
        fmt.Printf("Error executing task: %v\n", err)
        os.Exit(1) // Exit if task execution fails
    }
}

以上是初學者 Go 專案 - 在 Go 中建立任務運行程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
LibOffice在Debian上的安全設置怎麼做LibOffice在Debian上的安全設置怎麼做May 16, 2025 pm 01:24 PM

在Debian系統上確保整體安全性對於保護LibOffice等應用程序的運行環境至關重要。以下是一些提高系統安全性的通用建議:系統更新定期更新系統以修補已知的安全漏洞。 Debian12.10發布了安全更新,修復了大量安全漏洞,包括一些關鍵軟件包。用戶權限管理避免使用root用戶進行日常操作,以減少潛在的安全風險。建議創建普通用戶並加入sudo組,以限制對系統的直接訪問。 SSH服務安全配置使用SSH密鑰對進行身份認證,禁用root遠程登錄,並限制空密碼登錄。這些措施可以增強SSH服務的安全性,防止

Debian上如何配置Rust編譯選項Debian上如何配置Rust編譯選項May 16, 2025 pm 01:21 PM

在Debian系統上調整Rust編譯選項,可以通過多種途徑來實現,以下是幾種方法的詳細說明:使用rustup工具進行配置安裝rustup:若你尚未安裝rustup,可使用下述命令進行安裝:curl--proto'=https'--tlsv1.2-sSfhttps://sh.rustup.rs|sh依照提示完成安裝過程。設置編譯選項:rustup可用於為不同的工具鍊和目標配置編譯選項。你可以使用rustupoverride命令為特定項目設置編譯選項。例如,若想為某個項目設置特定的Rust版

Debian上Kubernetes節點如何管理Debian上Kubernetes節點如何管理May 16, 2025 pm 01:18 PM

在Debian系統上管理Kubernetes(K8S)節點通常涉及以下幾個關鍵步驟:1.安裝和配置Kubernetes組件準備工作:確保所有節點(包括主控節點和工作節點)都已安裝Debian操作系統,並且滿足安裝Kubernetes集群的基本要求,如足夠的CPU、內存和磁盤空間。禁用swap分區:為了確保kubelet能夠順利運行,建議禁用swap分區。設置防火牆規則:允許必要的端口,如kubelet、kube-apiserver、kube-scheduler等使用的端口。安裝container

Golang在Debian上的安全設置Golang在Debian上的安全設置May 16, 2025 pm 01:15 PM

在Debian上設置Golang環境時,確保系統安全是至關重要的。以下是一些關鍵的安全設置步驟和建議,幫助您構建一個安全的Golang開發環境:安全設置步驟系統更新:在安裝Golang之前,確保系統是最新的。使用以下命令更新系統軟件包列表和已安裝的軟件包:sudoaptupdatesudoaptupgrade-y防火牆配置:安裝並配置防火牆(如iptables)以限制對系統的訪問。僅允許必要的端口(如HTTP、HTTPS和SSH)連接。 sudoaptinstalliptablessud

Debian上Kubernetes部署的性能如何調優Debian上Kubernetes部署的性能如何調優May 16, 2025 pm 01:12 PM

在Debian上優化和部署Kubernetes集群的性能是一個涉及多個方面的複雜任務。以下是一些關鍵的優化策略和建議:硬件資源優化CPU:確保為Kubernetes節點和Pod分配足夠的CPU資源。內存:增加節點的內存容量,特別是對於內存密集型應用。存儲:使用高性能的SSD存儲,避免使用網絡文件系統(如NFS),因為它們可能會引入延遲。內核參數優化編輯/etc/sysctl.conf文件,添加或修改以下參數:net.core.somaxconn:65535net.ipv4.tcp_max_syn

Python腳本在Debian如何定時任務Python腳本在Debian如何定時任務May 16, 2025 pm 01:09 PM

在Debian系統中,你可以利用cron來安排定時任務,實現Python腳本的自動化執行。首先,啟動終端。通過輸入以下命令,編輯當前用戶的crontab文件:crontab-e如果需要以root權限編輯其他用戶的crontab文件,請使用:sudocrontab-uusername-e將username替換為你要編輯的用戶名。在crontab文件中,你可以添加定時任務,格式如下:*****/path/to/your/python-script.py這五個星號分別代表分鐘(0-59)、小

Debian如何配置Golang網絡參數Debian如何配置Golang網絡參數May 16, 2025 pm 01:06 PM

在Debian系統中調整Golang的網絡參數可以通過多種方式實現,以下是幾種可行的方法:方法一:通過設置環境變量臨時設置環境變量:在終端中輸入以下命令可以臨時設置環境變量,此設置僅在當前會話有效。 exportGODEBUG="gctrace=1netdns=go"其中,gctrace=1會激活垃圾回收跟踪,netdns=go則使Go使用其自身的DNS解析器而非系統默認的。永久設置環境變量:將上述命令添加到你的shell配置文件中,例如~/.bashrc或~/.profile

LibOffice在Debian上的快捷鍵有哪些LibOffice在Debian上的快捷鍵有哪些May 16, 2025 pm 01:03 PM

在Debian系統上自定義LibOffice的快捷鍵可以通過系統設置進行調整。以下是一些常用的步驟和方法來設置LibOffice的快捷鍵:設置LibOffice快捷鍵的基本步驟打開系統設置:在Debian系統中,點擊左上角的菜單(通常是一個齒輪圖標),然後選擇“系統設置”。選擇設備:在系統設置窗口中,選擇“設備”。選擇鍵盤:在設備設置頁面中,選擇“鍵盤”。找到對應工具的命令:在鍵盤設置頁面中,向下滾動到最底部可以看到“快捷鍵”選項,點擊它會彈出一個窗口。在彈出的窗口中找到對應LibOffice工

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。