Created
June 10, 2020 20:04
-
-
Save Noofbiz/bea98aa9a27c3a5ae0634e52f14b5481 to your computer and use it in GitHub Desktop.
Just hello world in a silly way idk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type builder struct { | |
workCh chan string | |
doneCh, quitCh chan struct{} | |
o string | |
} | |
func NewBuilder() *builder { | |
b := new(builder) | |
b.init() | |
return b | |
} | |
func (b *builder) init() { | |
b.workCh = make(chan string) | |
b.doneCh = make(chan struct{}) | |
b.quitCh = make(chan struct{}) | |
go func() { | |
for { | |
select { | |
case in := <- b.workCh: | |
if len(b.o) > 0 { | |
b.o += " " | |
} | |
b.o += in | |
case <- b.quitCh: | |
b.doneCh <- struct{}{} | |
return | |
} | |
} | |
}() | |
} | |
func (b *builder) Work(s string) { | |
b.workCh <- s | |
} | |
func (b *builder) Done() string { | |
b.quitCh <- struct{}{} | |
<- b.doneCh | |
return b.o | |
} | |
var ( | |
h = rune(104) | |
e = rune(101) | |
l = rune(108) | |
o = rune(111) | |
w = rune(119) | |
r = rune(114) | |
d = rune(100) | |
ex = rune(33) | |
) | |
const ( | |
wbit = 1 << iota | |
obit | |
rbit | |
lbit | |
dbit | |
exbit | |
) | |
type helloer interface { | |
Hello()string | |
} | |
type worlder interface { | |
World()string | |
} | |
type speaker struct { | |
b []rune | |
} | |
func NewSpeaker() *speaker { | |
s := new(speaker) | |
s.init() | |
return s | |
} | |
func (s *speaker) init() { | |
s.b = []rune{h, e, l, l, o} | |
} | |
func (s *speaker) Speak(i uint8) string { | |
ret := make([]rune, 0) | |
if i & wbit != 0 { | |
ret = append(ret, w) | |
} | |
if i & obit != 0 { | |
ret = append(ret, o) | |
} | |
if i & rbit != 0 { | |
ret = append(ret, r) | |
} | |
if i & lbit != 0 { | |
ret = append(ret, l) | |
} | |
if i & dbit != 0 { | |
ret = append(ret, d) | |
} | |
if i & exbit != 0 { | |
ret = append(ret, ex) | |
} | |
return string(ret) | |
} | |
func (s *speaker) Hello() string { | |
return string(s.b) | |
} | |
func (s *speaker) World() string { | |
return s.Speak(wbit | obit | rbit | lbit | dbit | exbit) | |
} | |
func main() { | |
b := NewBuilder() | |
s := NewSpeaker() | |
b.Work(s.Hello()) | |
b.Work(s.World()) | |
fmt.Println(b.Done()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment