package greetings
import "fmt"
// Hello returns a greeting for the named person.
func Hello(name string) string {
// Return a greeting that embeds the name in a message.
message := fmt.Sprintf("Hi, %v. Welcome!", name)
return message
}
In Go, a function whose name starts with a capital letter can be called by a function not in the same package. This is known in Go as an exported name.
In Go, the := operator is a shortcut for declaring and initializing a variable in one line (Go uses the value on the right to determine the variable’s type). Taking the long way, you might have written this as:
var message string
message = fmt.Sprintf("Hi, %v. Welcome!", name)
In Go, code executed as an application must go in a main package.
Returns two values: a string and an error. Your caller will check the second value to see if an error occurred. (Any Go function can return multiple values.)
Import the Go standard library errors package so you can use its errors.New function.
The errors.New function returns an error with your message inside.
Add nil (meaning no error) as a second value in the successful return. That way, the caller can see that the function succeeded.