Try implementing Yubaba in Go language

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.

Implementation

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! !!

Code commentary

I don't think the code is as difficult as I explain, but for the time being.

Standard output

Just use the built-in fmt package.

fmt.Println("It's a contract. Write your name there.")

Standard input

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()

Steal the name

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))]

crash

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

Try implementing Yubaba in Go language
Try implementing Yubaba in Python 3
Hello World in GO language
Try implementing perfume with Go
Use optinal type-like in Go language
Try implementing extension method in python
Post to slack in Go language
Write tests in GO language + gin
Do something object-oriented in GO language
Java programmer touched Go language (Implement Java inheritance in Go language)
Try implementing two stacks in one array in Python
I wrote the hexagonal architecture in go language
Try implementing the Monte Carlo method in Python
Create a web server in Go language (net/http) (2)
Try implementing Yubaba with Brainf * ck 512 lines (Generate and execute code in Python)
Write Pulumi in Go
Try to make a Python module in C language
Go language learning record
Go language environment construction
Try implementing associative memory with Hopfield network in Python
Try gRPC in Python
Go language cheat sheet
Features of Go language
Create a web server in Go language (net / http) (1)
Try 9 slices in Python
Go language to see and remember Part 7 C language in GO language
[Go language] How to get terminal input in real time
Try HeloWorld in your own language (with How to & code)
Machine language embedding in C language
Heapsort made in C language
Try implementing RBM with chainer.
Implement recursive closures in Go
Try implementing XOR with PyTorch
Programming language in "Hello World"
Try LINE Notify in Python
go language learning miscellaneous notes 1
[Golang] About Go language channel
Try to select a language
Created a package to support AWS Lambda development in Go language
Created gomi, a trash can tool for rm in Go language
Read the config file in Go language! Introducing a simple sample