Implementing Yubaba in Java (@ Nemesis) was buzzing, so It's completely piggybacking, but it seems to be fun, so I'll implement Go Auntie in Go language.
main.go
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strings"
"time"
)
func main() {
fmt.Println("It's a contract. Write your name there.")
stdin := bufio.NewScanner(os.Stdin)
stdin.Scan()
name := stdin.Text()
fmt.Printf("Hung.%Is it s? It's a luxurious name.\n", name)
nameArray := strings.Split(name, "")
rand.Seed(time.Now().UnixNano())
newname := nameArray[rand.Intn(len(nameArray))]
fmt.Printf("From now on your name is%s. Mind you,%It's s. I'll reply when I understand%s!!\n", newname, newname, newname)
}
If you have a Go runtime, run main.go and it will work.
$ go run main.go
It's a contract. Write your name there.
Yamada Taro
Hung. Is it Taro Yamada? It's a luxurious name.
From now on your name is mountain. It ’s a mountain. If you find out, I'll reply, mountain! !!
I don't think the code is as difficult as I explain, but for the time being.
Just use the built-in fmt package.
fmt.Println("It's a contract. Write your name there.")
I'm using the bufio package. There are multiple standard input methods in Go, and using fmt.Scan has the least amount of code, but Not adopted because it does not accept uninput returns (because I wanted to reproduce the crash without input).
stdin := bufio.NewScanner(os.Stdin)
stdin.Scan()
name := stdin.Text()
The character string received from the standard input is converted into an array by split, and only one character is extracted by specifying the index using random numbers. Math / rand is used to create random numbers.
nameArray := strings.Split(name, "")
rand.Seed(time.Now().UnixNano())
newname := nameArray[rand.Intn(len(nameArray))]
If you do not input anything with standard input, it will panic.
I also crashed it at the head family, so I tried to reproduce it.
It's an error that you shouldn't specify 0 for the argument of rand.Intn
.
panic: invalid argument to Intn
goroutine 1 [running]:
math/rand.(*Rand).Intn(0xc000058180, 0x0, 0x1188ce0)
/usr/local/opt/go/libexec/src/math/rand/rand.go:169 +0x9c
math/rand.Intn(...)
/usr/local/opt/go/libexec/src/math/rand/rand.go:337
main.main()
/Users/nakaya/go/src/yubaba/main.go:22 +0x2ad
exit status 2
By the way, to prevent it from crashing and not accepting input with only empty strings, do the following.
main.go
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strings"
"time"
)
func main() {
var name string
for len(strings.TrimSpace(name)) == 0 {
fmt.Println("It's a contract. Write your name there.")
stdin := bufio.NewScanner(os.Stdin)
stdin.Scan()
name = stdin.Text()
}
fmt.Printf("Hung.%Is it s? It's a luxurious name.\n", name)
nameArray := strings.Split(name, "")
rand.Seed(time.Now().UnixNano())
newname := nameArray[rand.Intn(len(nameArray))]
fmt.Printf("From now on your name is%s. Mind you,%It's s. I'll reply when I understand%s!!\n", newname, newname, newname)
}
Yubaba loops endlessly until there is some character input.
$ go run main.go
It's a contract. Write your name there.
It's a contract. Write your name there.
It's a contract. Write your name there.
It's a contract. Write your name there.
It's a contract. Write your name there.
Recommended Posts