sf
This commit is contained in:
parent
23931220bb
commit
0e1294eb8e
3
book_with_bridge/goroutines/clock_server/go.mod
Normal file
3
book_with_bridge/goroutines/clock_server/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module main
|
||||
|
||||
go 1.25.5
|
||||
46
book_with_bridge/goroutines/clock_server/main.go
Normal file
46
book_with_bridge/goroutines/clock_server/main.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
type newWriter struct {
|
||||
conn io.Writer
|
||||
}
|
||||
|
||||
func (nw newWriter) Write(p []byte) (n int, err error) {
|
||||
prefix := []byte("curr time is ")
|
||||
prefix = append(prefix, p...)
|
||||
n, err = nw.conn.Write(prefix)
|
||||
return
|
||||
}
|
||||
|
||||
func handleConn(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
|
||||
for {
|
||||
_, err := io.WriteString(newWriter{conn}, time.Now().Format("15:04:05\n"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
listener, err := net.Listen("tcp", ":9090")
|
||||
if err != nil {
|
||||
log.Fatalf("Shit happened %s",err)
|
||||
}
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
log.Printf("Error is %s",err)
|
||||
continue
|
||||
}
|
||||
go handleConn(conn)
|
||||
}
|
||||
}
|
||||
3
book_with_bridge/goroutines/spinner/go.mod
Normal file
3
book_with_bridge/goroutines/spinner/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module main
|
||||
|
||||
go 1.25.5
|
||||
15
book_with_bridge/goroutines/spinner/main.go
Normal file
15
book_with_bridge/goroutines/spinner/main.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for {
|
||||
for _,r := range `-\|/` {
|
||||
fmt.Printf("\r processing in progress %c",r)
|
||||
time.Sleep(50*time.Millisecond)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user