-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction03.go
More file actions
39 lines (30 loc) · 1.17 KB
/
function03.go
File metadata and controls
39 lines (30 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* RZFeeser | Alta3 Research
Errors are "just" a type that can be returned by functions. This simple approach
might be a bit different than other languages you've used.
Errors can be constructed on the fly using Go’s built-in errors or fmt packages.
In this example we look at using, errors.New().
See documentation @ https://pkg.go.dev/errors
errors.New constructs a basic error value with given message */
package main
import (
"errors" // preview of using errors
"fmt"
)
//name of funct //input //output
func rollchar(firstName string, lastName string) (string, error) {
// generate an error
if lastName == "Turnip" || lastName == "Radish" || lastName == "Potato" {
return lastName, errors.New("Vegetables are not suitable last names for heroes.")
}
// desirable response
return firstName + " the " + lastName, nil
}
func main() {
fmt.Println("Welcome to the Character Generator")
playerChar, err := rollchar("Gandalf", "superman")
if err != nil {
fmt.Println("Error while spawning your requested character.")
} else {
fmt.Println(playerChar, "has been generated.")
}
}