Hi, can you make me an GO CLI-Tool which need tree args. first arg is "--leistung",…
- User
Hi, can you make me an GO CLI-Tool which need tree args. first arg is "--leistung", second is "--strompreis", and the last is "--stromverbrauch"
- Assistant
Certainly! Below is an example Go CLI tool that uses three arguments (
--leistung,--strompreis, and--stromverbrauch) as you requested. This example CLI tool will calculate the cost of electricity consumed based on the provided arguments.package main import ( "flag" "fmt" "os" "strconv" ) func main() { // Defining flags leistungFlag := flag.String("leistung", "", "Specify the performance in kW") strompreisFlag := flag.String("strompreis", "", "Specify the electricity price in cents per kWh") stromverbrauchFlag := flag.String("stromverbrauch", "", "Specify the electricity consumption in kWh") // Parse the flags flag.Parse() // Convert flag values to appropriate types leistung, err := strconv.ParseFloat(*leistungFlag, 64) if err != nil { fmt.Println("Invalid value for --leistung. Please provide a valid number.") os.Exit(1) } strompreis, err := strconv.ParseFloat(*strompreisFlag, 64) if err != nil { fmt.Println("Invalid value for --strompreis. Please provide a valid number.") os.Exit(1) } stromverbrauch, err := strconv.ParseFloat(*stromverbrauchFlag, 64) if err != nil { fmt.Println("Invalid value for --stromverbrauch. Please provide a valid number.") os.Exit(1) } // Calculate the cost of electricity consumed cost := calculateElectricityCost(leistung, strompreis, stromverbrauch) // Print output fmt.Printf("The cost of electricity consumed is: %.2f cents\n", cost) } // calculateElectricityCost calculates the cost of electricity consumed. func calculateElectricityCost(leistung float64, strompreis float64, stromverbrauch float64) float64 { // Example calculation // Note: Adjust the formula according to your needs. // This example simply multiplies the electricity price by consumption. // The "leistung" parameter is not used in this example calculation, but you can modify the function to include it as needed. cost := strompreis * stromverbrauch return cost }To run this program, you would first need to compile it with
go build, then you can execute the compiled binary with the arguments like this:./programName --leistung 100 --strompreis 30 --stromverbrauch 200Replace
programNamewith the name of your compiled binary. This command will calculate the cost of electricity using the provided--strompreisand--stromverbrauch, assuming the price is in cents and consumption in kWh.This example keeps things simple for brevity and clarity, and it might be adjusted or extended based on the specific requirements or computations you have in mind.