Compare commits
2 Commits
6633420cf6
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| a8496afb35 | |||
| ea348b07a6 |
34
channel/mutex/basic/main.go
Normal file
34
channel/mutex/basic/main.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Sth struct {
|
||||||
|
x int64
|
||||||
|
wg sync.WaitGroup
|
||||||
|
lock sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sth) add() {
|
||||||
|
for i := 0; i < 10000; i++ {
|
||||||
|
s.lock.Lock()
|
||||||
|
s.x++
|
||||||
|
s.lock.Unlock()
|
||||||
|
}
|
||||||
|
s.wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var s Sth
|
||||||
|
s.wg.Add(2)
|
||||||
|
go s.add()
|
||||||
|
go s.add()
|
||||||
|
s.wg.Wait()
|
||||||
|
fmt.Println(s)
|
||||||
|
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
fmt.Println("xiaowei zhaodandan")
|
||||||
|
}
|
||||||
47
channel/mutex/rwlock/main.go
Normal file
47
channel/mutex/rwlock/main.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
x int64
|
||||||
|
wg sync.WaitGroup
|
||||||
|
rwlock sync.RWMutex
|
||||||
|
lock sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
start := time.Now()
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go write()
|
||||||
|
}
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go read()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
fmt.Println("cost:", time.Since(start))
|
||||||
|
}
|
||||||
|
|
||||||
|
func read() {
|
||||||
|
//lock.Lock()
|
||||||
|
rwlock.RLock()
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
//lock.Unlock()
|
||||||
|
rwlock.RUnlock()
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
func write() {
|
||||||
|
rwlock.Lock()
|
||||||
|
//lock.Lock()
|
||||||
|
x = x + 1
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
//lock.Unlock()
|
||||||
|
rwlock.Unlock()
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
@@ -13,9 +13,13 @@ func main() {
|
|||||||
c.String(200, "hello,%v", c.Query("name"))
|
c.String(200, "hello,%v", c.Query("name"))
|
||||||
})
|
})
|
||||||
engine.POST("/login", func(c *gee.Context) {
|
engine.POST("/login", func(c *gee.Context) {
|
||||||
|
//c.JSON(200, gee.H{
|
||||||
|
// "username": "xxxx",
|
||||||
|
// "password": "1234",
|
||||||
|
//})
|
||||||
c.JSON(200, gee.H{
|
c.JSON(200, gee.H{
|
||||||
"username": "xxxx",
|
"username": c.PostForm("username"),
|
||||||
"password": "1234",
|
"password": c.PostForm("password"),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
_ = engine.Run(":9000")
|
_ = engine.Run(":9000")
|
||||||
|
|||||||
@@ -1 +1,46 @@
|
|||||||
package gee
|
package gee
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HandlerFunc func(w http.ResponseWriter, r *http.Request)
|
||||||
|
|
||||||
|
type Engine struct {
|
||||||
|
router map[string]HandlerFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *Engine {
|
||||||
|
return &Engine{
|
||||||
|
router: make(map[string]HandlerFunc),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) addRoute(method string, pattern string, handler HandlerFunc) {
|
||||||
|
key := method + "-" + pattern
|
||||||
|
e.router[key] = handler
|
||||||
|
log.Println(e.router)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) GET(pattern string, handler HandlerFunc) {
|
||||||
|
e.addRoute("GET", pattern, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) POST(pattern string, handler HandlerFunc) {
|
||||||
|
e.addRoute("POST", pattern, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) Run(addr string) error {
|
||||||
|
return http.ListenAndServe(addr, e)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
key := request.Method + "-" + request.URL.Path
|
||||||
|
if handler, ok := e.router[key]; ok {
|
||||||
|
handler(writer, request)
|
||||||
|
} else {
|
||||||
|
_, _ = fmt.Fprintf(writer, "404 not found:%v\n", request.URL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1 +1,19 @@
|
|||||||
package framework
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"golanglearn/web/framework/framework/gee"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
engine := gee.New()
|
||||||
|
engine.GET("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintf(w, "hello web frame work")
|
||||||
|
})
|
||||||
|
|
||||||
|
engine.GET("/hello", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintf(w, "hello web frame work")
|
||||||
|
})
|
||||||
|
engine.Run(":9000")
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ type Engine struct {
|
|||||||
|
|
||||||
// 实现Handler的ServerHTTP方法
|
// 实现Handler的ServerHTTP方法
|
||||||
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.RequestURI {
|
switch r.URL.Path {
|
||||||
case "/":
|
case "/":
|
||||||
fmt.Fprint(w, "hello handler-basic")
|
fmt.Fprint(w, "hello handler-basic")
|
||||||
case "/web":
|
case "/web":
|
||||||
|
|||||||
Reference in New Issue
Block a user