Errors


Generate error messages with fmt.Errorf.

package main

import (
    "fmt"
)

func beer_for(str string) (string, error) {
    if str == "Johnny" {
        return "Johnny", fmt.Errorf("Bad Johnny! No beer for you.")
    } else {
        return "HI " + str + ", here's a beer!", nil
    }
}

func main() {
    str, err := beer_for("Johnny")
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(str)
    }

    str, err = beer_for("Ricky")
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(str)
    }
}

Output

Bad Johnny! No beer for you.
HI Ricky, here's a beer!

results matching ""

    No results matching ""