updatges
This commit is contained in:
parent
fd67d524c1
commit
5c8ddea5ff
@ -1,30 +0,0 @@
|
||||
package main
|
||||
|
||||
import ("fmt"; "net")
|
||||
|
||||
func client() {
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
c, err := net.Dial("tcp", "127.0.0.1:5000")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
msg := "Hello World"
|
||||
fmt.Println("Sending: ", msg)
|
||||
_, err = c.Write([]byte(msg))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
buf := make([]byte, 1024)
|
||||
_, err = c.Read(buf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
fmt.Println("Received:", string(buf))
|
||||
c.Close()
|
||||
}
|
||||
}
|
27
088-tcp-client/tcp-client.go
Normal file
27
088-tcp-client/tcp-client.go
Normal file
@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import "net"
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
c, err := net.Dial("tcp", "127.0.0.1:5000")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
msg := "hello world"
|
||||
fmt.Println("Sending: ", msg)
|
||||
_, err = c.Write([]byte(msg))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
buf := make([]byte, 1024)
|
||||
_, err = c.Read(buf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
fmt.Println("Received:", string(buf))
|
||||
c.Close()
|
||||
}
|
||||
}
|
3
088-tcp-client/tcp-client.sh
Normal file
3
088-tcp-client/tcp-client.sh
Normal file
@ -0,0 +1,3 @@
|
||||
$ go run tcp-client.go
|
||||
Sending: hello world
|
||||
Received: hello world
|
@ -1,24 +0,0 @@
|
||||
package main
|
||||
|
||||
import ("net")
|
||||
|
||||
func main() {
|
||||
listener, _ := net.Listen("tcp", "0.0.0.0:5000")
|
||||
for {
|
||||
conn, _ := listener.Accept()
|
||||
go Serve(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func Serve(conn net.Conn) {
|
||||
buf := make([]byte, 1024)
|
||||
for {
|
||||
_, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return
|
||||
} else {
|
||||
conn.Write(buf)
|
||||
}
|
||||
}
|
||||
}
|
26
089-tcp-server/tcp-server.go
Normal file
26
089-tcp-server/tcp-server.go
Normal file
@ -0,0 +1,26 @@
|
||||
// ## TCP Server
|
||||
|
||||
package main
|
||||
|
||||
import "net"
|
||||
|
||||
func main() {
|
||||
listener, _ := net.Listen("tcp", "0.0.0.0:5000")
|
||||
for {
|
||||
conn, _ := listener.Accept()
|
||||
go Serve(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func Serve(conn net.Conn) {
|
||||
buf := make([]byte, 1024)
|
||||
for {
|
||||
_, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return
|
||||
} else {
|
||||
conn.Write(buf)
|
||||
}
|
||||
}
|
||||
}
|
1
089-tcp-server/tcp-server.sh
Normal file
1
089-tcp-server/tcp-server.sh
Normal file
@ -0,0 +1 @@
|
||||
$ go run tcp-server.go
|
@ -1,15 +0,0 @@
|
||||
package main
|
||||
|
||||
import ("time"; "fmt")
|
||||
|
||||
func main() {
|
||||
ticker := time.NewTicker(time.Millisecond * 500)
|
||||
go func() {
|
||||
for t := range ticker.C {
|
||||
fmt.Println("Tick at", t)
|
||||
}
|
||||
}()
|
||||
time.Sleep(time.Millisecond * 1500)
|
||||
ticker.Stop()
|
||||
fmt.Println("Ticker stopped")
|
||||
}
|
18
090-tickers/tickers.go
Normal file
18
090-tickers/tickers.go
Normal file
@ -0,0 +1,18 @@
|
||||
// ## Tickers
|
||||
|
||||
package main
|
||||
|
||||
import "time"
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
ticker := time.NewTicker(time.Millisecond * 500)
|
||||
go func() {
|
||||
for t := range ticker.C {
|
||||
fmt.Println("Tick at", t)
|
||||
}
|
||||
}()
|
||||
time.Sleep(time.Millisecond * 1500)
|
||||
ticker.Stop()
|
||||
fmt.Println("Ticker stopped")
|
||||
}
|
5
090-tickers/tickers.sh
Normal file
5
090-tickers/tickers.sh
Normal file
@ -0,0 +1,5 @@
|
||||
$ go run tickers.go
|
||||
Tick at 2012-09-23 11:29:56.487625 -0700 PDT
|
||||
Tick at 2012-09-23 11:29:56.988063 -0700 PDT
|
||||
Tick at 2012-09-23 11:29:57.488076 -0700 PDT
|
||||
Ticker stopped
|
@ -1,19 +0,0 @@
|
||||
package main
|
||||
|
||||
import ("time"; "fmt")
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
fmt.Println(now)
|
||||
|
||||
then := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
|
||||
fmt.Println(then)
|
||||
|
||||
diff := now.Sub(then)
|
||||
fmt.Println(diff)
|
||||
}
|
||||
|
||||
// == todo
|
||||
// extract parts
|
||||
// add duration
|
||||
// check before / after
|
21
091-time/time.go
Normal file
21
091-time/time.go
Normal file
@ -0,0 +1,21 @@
|
||||
// ## Time
|
||||
|
||||
package main
|
||||
|
||||
import "time"
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
fmt.Println(now)
|
||||
|
||||
then := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
|
||||
fmt.Println(then)
|
||||
|
||||
diff := now.Sub(then)
|
||||
fmt.Println(diff)
|
||||
}
|
||||
|
||||
// todo: extract parts
|
||||
// todo: add duration
|
||||
// todo: check before / after
|
4
091-time/time.sh
Normal file
4
091-time/time.sh
Normal file
@ -0,0 +1,4 @@
|
||||
$ go run time.go
|
||||
2012-09-23 11:28:59.551605 -0700 PDT
|
||||
2009-11-17 20:34:58.651387237 +0000 UTC
|
||||
24981h54m0.900217763s
|
Loading…
x
Reference in New Issue
Block a user