-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (60 loc) · 1.26 KB
/
main.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/************************
Created by: Mitchell Noun
Date created: 2/20/22
Class: COMP415 Emerging Languages
Assignment: Project 2
*************************/
package main
import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"strings"
)
func main() {
//create channel and spawn the fortune function with the channel as a param
message := make(chan string)
fmt.Println("Your fortune is: ")
go fortune(message)
{
message <- "true"
}
//for loop until user responds no
for true {
fmt.Println("Do you want another fortune? (yes/no)")
var response string
fmt.Scanln(&response) //gets input from user
response = strings.ToLower(response)
//if yes, send another fortune
if response == "yes" {
go fortune(message)
{
message <- "true"
}
}
//if no, break out of loop
if response == "no" {
break
}
}
fmt.Println("Program has ended")
return
}
func fortune(message chan string) {
//open and read fortunes.txt
content, err := ioutil.ReadFile("Fortunes.txt")
if err != nil {
log.Fatal(err)
}
//split the contents of file
contentSlice := strings.Split(string(content), "%%")
msg := <-message
//for message channel
for msg == "true" {
n := len(contentSlice)
r := rand.Intn(n) //picking a random fortune
println(contentSlice[r])
break
}
}