Hello world done
parent
441b3a9d54
commit
ca7d449b44
@ -1,12 +1,49 @@
|
|||||||
|
// Provides a Hello world example with tests
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Constants can be grouped in a block.
|
||||||
|
// Add a line between related consts for
|
||||||
|
// readbility
|
||||||
|
const (
|
||||||
|
french = "French"
|
||||||
|
spanish = "Spanish"
|
||||||
|
|
||||||
|
englishHelloPrefix = "Hello, "
|
||||||
|
spanishHelloPrefix = "Hola, "
|
||||||
|
frenchHelloPrefix = "Bonjour, "
|
||||||
|
)
|
||||||
|
|
||||||
// Hello prints the string "Hello, world"
|
// Hello prints the string "Hello, world"
|
||||||
func Hello(name string) string {
|
func Hello(name string, language string) string {
|
||||||
return "Hello, " + name
|
if name == "" {
|
||||||
|
name = "World"
|
||||||
|
}
|
||||||
|
|
||||||
|
return greetingPrefix(language) + name
|
||||||
|
}
|
||||||
|
|
||||||
|
// greetingPrefix Using a named return value will initialize a "zero value"
|
||||||
|
// variable which as you can see below can be assigned to.
|
||||||
|
// this will display in Go Doc for your function.
|
||||||
|
// Lower case letter functions are "private" functions. They are internal to implementation.
|
||||||
|
func greetingPrefix(language string) (prefix string) {
|
||||||
|
switch language {
|
||||||
|
case french:
|
||||||
|
prefix = frenchHelloPrefix
|
||||||
|
case spanish:
|
||||||
|
prefix = spanishHelloPrefix
|
||||||
|
default:
|
||||||
|
prefix = englishHelloPrefix
|
||||||
|
}
|
||||||
|
// we use names return value in function signature
|
||||||
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println(Hello("world"))
|
fmt.Println(Hello("world", ""))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue