type Status struct { Code int Message string Color string }
var ( StatusPending = Status{Code: 1, Message: "Pending", Color: "yellow"} StatusRunning = Status{Code: 2, Message: "Running", Color: "blue"} StatusSuccess = Status{Code: 3, Message: "Success", Color: "green"} StatusFailed = Status{Code: 4, Message: "Failed", Color: "red"} )
var Statuses = []Status{ StatusPending, StatusRunning, StatusSuccess, StatusFailed, }
func StatusFromCode(code int) (Status, bool) { for _, status := range Statuses { if status.Code == code { return status, true } } return Status{}, false }
func StatusFromMessage(message string) (Status, bool) { for _, status := range Statuses { if status.Message == message { return status, true } } return Status{}, false }
func main() { currentStatus := StatusRunning fmt.Printf("Status: %s (Code: %d, Color: %s)\n", currentStatus.Message, currentStatus.Code, currentStatus.Color) if status, found := StatusFromCode(3); found { fmt.Printf("Found status: %s\n", status.Message) } }
|