1d8afdc9 by astaxie

gofmt -s & go_vet

1 parent 1592e9c0
...@@ -202,7 +202,7 @@ func (bc *MemoryCache) vaccuum() { ...@@ -202,7 +202,7 @@ func (bc *MemoryCache) vaccuum() {
202 if bc.items == nil { 202 if bc.items == nil {
203 return 203 return
204 } 204 }
205 for name, _ := range bc.items { 205 for name := range bc.items {
206 bc.item_expired(name) 206 bc.item_expired(name)
207 } 207 }
208 } 208 }
......
...@@ -324,7 +324,7 @@ func (d *dbBase) Read(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Lo ...@@ -324,7 +324,7 @@ func (d *dbBase) Read(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Lo
324 query := fmt.Sprintf("SELECT %s%s%s FROM %s%s%s WHERE %s%s%s = ?", Q, sels, Q, Q, mi.table, Q, Q, wheres, Q) 324 query := fmt.Sprintf("SELECT %s%s%s FROM %s%s%s WHERE %s%s%s = ?", Q, sels, Q, Q, mi.table, Q, Q, wheres, Q)
325 325
326 refs := make([]interface{}, colsNum) 326 refs := make([]interface{}, colsNum)
327 for i, _ := range refs { 327 for i := range refs {
328 var ref interface{} 328 var ref interface{}
329 refs[i] = &ref 329 refs[i] = &ref
330 } 330 }
...@@ -423,7 +423,7 @@ func (d *dbBase) InsertValue(q dbQuerier, mi *modelInfo, isMulti bool, names []s ...@@ -423,7 +423,7 @@ func (d *dbBase) InsertValue(q dbQuerier, mi *modelInfo, isMulti bool, names []s
423 Q := d.ins.TableQuote() 423 Q := d.ins.TableQuote()
424 424
425 marks := make([]string, len(names)) 425 marks := make([]string, len(names))
426 for i, _ := range marks { 426 for i := range marks {
427 marks[i] = "?" 427 marks[i] = "?"
428 } 428 }
429 429
...@@ -693,7 +693,7 @@ func (d *dbBase) DeleteBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con ...@@ -693,7 +693,7 @@ func (d *dbBase) DeleteBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
693 } 693 }
694 694
695 marks := make([]string, len(args)) 695 marks := make([]string, len(args))
696 for i, _ := range marks { 696 for i := range marks {
697 marks[i] = "?" 697 marks[i] = "?"
698 } 698 }
699 sql := fmt.Sprintf("IN (%s)", strings.Join(marks, ", ")) 699 sql := fmt.Sprintf("IN (%s)", strings.Join(marks, ", "))
...@@ -824,7 +824,7 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi ...@@ -824,7 +824,7 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
824 } 824 }
825 825
826 refs := make([]interface{}, colsNum) 826 refs := make([]interface{}, colsNum)
827 for i, _ := range refs { 827 for i := range refs {
828 var ref interface{} 828 var ref interface{}
829 refs[i] = &ref 829 refs[i] = &ref
830 } 830 }
...@@ -964,7 +964,7 @@ func (d *dbBase) GenerateOperatorSql(mi *modelInfo, fi *fieldInfo, operator stri ...@@ -964,7 +964,7 @@ func (d *dbBase) GenerateOperatorSql(mi *modelInfo, fi *fieldInfo, operator stri
964 switch operator { 964 switch operator {
965 case "in": 965 case "in":
966 marks := make([]string, len(params)) 966 marks := make([]string, len(params))
967 for i, _ := range marks { 967 for i := range marks {
968 marks[i] = "?" 968 marks[i] = "?"
969 } 969 }
970 sql = fmt.Sprintf("IN (%s)", strings.Join(marks, ", ")) 970 sql = fmt.Sprintf("IN (%s)", strings.Join(marks, ", "))
...@@ -1460,7 +1460,7 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond ...@@ -1460,7 +1460,7 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond
1460 } 1460 }
1461 1461
1462 refs := make([]interface{}, len(cols)) 1462 refs := make([]interface{}, len(cols))
1463 for i, _ := range refs { 1463 for i := range refs {
1464 var ref interface{} 1464 var ref interface{}
1465 refs[i] = &ref 1465 refs[i] = &ref
1466 } 1466 }
......
...@@ -242,14 +242,14 @@ type User struct { ...@@ -242,14 +242,14 @@ type User struct {
242 242
243 func (u *User) TableIndex() [][]string { 243 func (u *User) TableIndex() [][]string {
244 return [][]string{ 244 return [][]string{
245 []string{"Id", "UserName"}, 245 {"Id", "UserName"},
246 []string{"Id", "Created"}, 246 {"Id", "Created"},
247 } 247 }
248 } 248 }
249 249
250 func (u *User) TableUnique() [][]string { 250 func (u *User) TableUnique() [][]string {
251 return [][]string{ 251 return [][]string{
252 []string{"UserName", "Email"}, 252 {"UserName", "Email"},
253 } 253 }
254 } 254 }
255 255
...@@ -287,7 +287,7 @@ type Post struct { ...@@ -287,7 +287,7 @@ type Post struct {
287 287
288 func (u *Post) TableIndex() [][]string { 288 func (u *Post) TableIndex() [][]string {
289 return [][]string{ 289 return [][]string{
290 []string{"Id", "Created"}, 290 {"Id", "Created"},
291 } 291 }
292 } 292 }
293 293
......
...@@ -585,7 +585,7 @@ func (o *rawSet) readValues(container interface{}, needCols []string) (int64, er ...@@ -585,7 +585,7 @@ func (o *rawSet) readValues(container interface{}, needCols []string) (int64, er
585 585
586 cols = columns 586 cols = columns
587 refs = make([]interface{}, len(cols)) 587 refs = make([]interface{}, len(cols))
588 for i, _ := range refs { 588 for i := range refs {
589 var ref sql.NullString 589 var ref sql.NullString
590 refs[i] = &ref 590 refs[i] = &ref
591 591
...@@ -711,7 +711,7 @@ func (o *rawSet) queryRowsTo(container interface{}, keyCol, valueCol string) (in ...@@ -711,7 +711,7 @@ func (o *rawSet) queryRowsTo(container interface{}, keyCol, valueCol string) (in
711 } else { 711 } else {
712 cols = columns 712 cols = columns
713 refs = make([]interface{}, len(cols)) 713 refs = make([]interface{}, len(cols))
714 for i, _ := range refs { 714 for i := range refs {
715 if keyCol == cols[i] { 715 if keyCol == cols[i] {
716 keyIndex = i 716 keyIndex = i
717 } 717 }
......
...@@ -586,29 +586,29 @@ func TestInsertTestData(t *testing.T) { ...@@ -586,29 +586,29 @@ func TestInsertTestData(t *testing.T) {
586 throwFail(t, AssertIs(id, 4)) 586 throwFail(t, AssertIs(id, 4))
587 587
588 tags := []*Tag{ 588 tags := []*Tag{
589 &Tag{Name: "golang", BestPost: &Post{Id: 2}}, 589 {Name: "golang", BestPost: &Post{Id: 2}},
590 &Tag{Name: "example"}, 590 {Name: "example"},
591 &Tag{Name: "format"}, 591 {Name: "format"},
592 &Tag{Name: "c++"}, 592 {Name: "c++"},
593 } 593 }
594 594
595 posts := []*Post{ 595 posts := []*Post{
596 &Post{User: users[0], Tags: []*Tag{tags[0]}, Title: "Introduction", Content: `Go is a new language. Although it borrows ideas from existing languages, it has unusual properties that make effective Go programs different in character from programs written in its relatives. A straightforward translation of a C++ or Java program into Go is unlikely to produce a satisfactory result—Java programs are written in Java, not Go. On the other hand, thinking about the problem from a Go perspective could produce a successful but quite different program. In other words, to write Go well, it's important to understand its properties and idioms. It's also important to know the established conventions for programming in Go, such as naming, formatting, program construction, and so on, so that programs you write will be easy for other Go programmers to understand. 596 {User: users[0], Tags: []*Tag{tags[0]}, Title: "Introduction", Content: `Go is a new language. Although it borrows ideas from existing languages, it has unusual properties that make effective Go programs different in character from programs written in its relatives. A straightforward translation of a C++ or Java program into Go is unlikely to produce a satisfactory result—Java programs are written in Java, not Go. On the other hand, thinking about the problem from a Go perspective could produce a successful but quite different program. In other words, to write Go well, it's important to understand its properties and idioms. It's also important to know the established conventions for programming in Go, such as naming, formatting, program construction, and so on, so that programs you write will be easy for other Go programmers to understand.
597 This document gives tips for writing clear, idiomatic Go code. It augments the language specification, the Tour of Go, and How to Write Go Code, all of which you should read first.`}, 597 This document gives tips for writing clear, idiomatic Go code. It augments the language specification, the Tour of Go, and How to Write Go Code, all of which you should read first.`},
598 &Post{User: users[1], Tags: []*Tag{tags[0], tags[1]}, Title: "Examples", Content: `The Go package sources are intended to serve not only as the core library but also as examples of how to use the language. Moreover, many of the packages contain working, self-contained executable examples you can run directly from the golang.org web site, such as this one (click on the word "Example" to open it up). If you have a question about how to approach a problem or how something might be implemented, the documentation, code and examples in the library can provide answers, ideas and background.`}, 598 {User: users[1], Tags: []*Tag{tags[0], tags[1]}, Title: "Examples", Content: `The Go package sources are intended to serve not only as the core library but also as examples of how to use the language. Moreover, many of the packages contain working, self-contained executable examples you can run directly from the golang.org web site, such as this one (click on the word "Example" to open it up). If you have a question about how to approach a problem or how something might be implemented, the documentation, code and examples in the library can provide answers, ideas and background.`},
599 &Post{User: users[1], Tags: []*Tag{tags[0], tags[2]}, Title: "Formatting", Content: `Formatting issues are the most contentious but the least consequential. People can adapt to different formatting styles but it's better if they don't have to, and less time is devoted to the topic if everyone adheres to the same style. The problem is how to approach this Utopia without a long prescriptive style guide. 599 {User: users[1], Tags: []*Tag{tags[0], tags[2]}, Title: "Formatting", Content: `Formatting issues are the most contentious but the least consequential. People can adapt to different formatting styles but it's better if they don't have to, and less time is devoted to the topic if everyone adheres to the same style. The problem is how to approach this Utopia without a long prescriptive style guide.
600 With Go we take an unusual approach and let the machine take care of most formatting issues. The gofmt program (also available as go fmt, which operates at the package level rather than source file level) reads a Go program and emits the source in a standard style of indentation and vertical alignment, retaining and if necessary reformatting comments. If you want to know how to handle some new layout situation, run gofmt; if the answer doesn't seem right, rearrange your program (or file a bug about gofmt), don't work around it.`}, 600 With Go we take an unusual approach and let the machine take care of most formatting issues. The gofmt program (also available as go fmt, which operates at the package level rather than source file level) reads a Go program and emits the source in a standard style of indentation and vertical alignment, retaining and if necessary reformatting comments. If you want to know how to handle some new layout situation, run gofmt; if the answer doesn't seem right, rearrange your program (or file a bug about gofmt), don't work around it.`},
601 &Post{User: users[2], Tags: []*Tag{tags[3]}, Title: "Commentary", Content: `Go provides C-style /* */ block comments and C++-style // line comments. Line comments are the norm; block comments appear mostly as package comments, but are useful within an expression or to disable large swaths of code. 601 {User: users[2], Tags: []*Tag{tags[3]}, Title: "Commentary", Content: `Go provides C-style /* */ block comments and C++-style // line comments. Line comments are the norm; block comments appear mostly as package comments, but are useful within an expression or to disable large swaths of code.
602 The program—and web server—godoc processes Go source files to extract documentation about the contents of the package. Comments that appear before top-level declarations, with no intervening newlines, are extracted along with the declaration to serve as explanatory text for the item. The nature and style of these comments determines the quality of the documentation godoc produces.`}, 602 The program—and web server—godoc processes Go source files to extract documentation about the contents of the package. Comments that appear before top-level declarations, with no intervening newlines, are extracted along with the declaration to serve as explanatory text for the item. The nature and style of these comments determines the quality of the documentation godoc produces.`},
603 } 603 }
604 604
605 comments := []*Comment{ 605 comments := []*Comment{
606 &Comment{Post: posts[0], Content: "a comment"}, 606 {Post: posts[0], Content: "a comment"},
607 &Comment{Post: posts[1], Content: "yes"}, 607 {Post: posts[1], Content: "yes"},
608 &Comment{Post: posts[1]}, 608 {Post: posts[1]},
609 &Comment{Post: posts[1]}, 609 {Post: posts[1]},
610 &Comment{Post: posts[2]}, 610 {Post: posts[2]},
611 &Comment{Post: posts[2]}, 611 {Post: posts[2]},
612 } 612 }
613 613
614 for _, tag := range tags { 614 for _, tag := range tags {
...@@ -1248,7 +1248,7 @@ func TestQueryM2M(t *testing.T) { ...@@ -1248,7 +1248,7 @@ func TestQueryM2M(t *testing.T) {
1248 post := Post{Id: 4} 1248 post := Post{Id: 4}
1249 m2m := dORM.QueryM2M(&post, "Tags") 1249 m2m := dORM.QueryM2M(&post, "Tags")
1250 1250
1251 tag1 := []*Tag{&Tag{Name: "TestTag1"}, &Tag{Name: "TestTag2"}} 1251 tag1 := []*Tag{{Name: "TestTag1"}, {Name: "TestTag2"}}
1252 tag2 := &Tag{Name: "TestTag3"} 1252 tag2 := &Tag{Name: "TestTag3"}
1253 tag3 := []interface{}{&Tag{Name: "TestTag4"}} 1253 tag3 := []interface{}{&Tag{Name: "TestTag4"}}
1254 1254
...@@ -1311,7 +1311,7 @@ func TestQueryM2M(t *testing.T) { ...@@ -1311,7 +1311,7 @@ func TestQueryM2M(t *testing.T) {
1311 1311
1312 m2m = dORM.QueryM2M(&tag, "Posts") 1312 m2m = dORM.QueryM2M(&tag, "Posts")
1313 1313
1314 post1 := []*Post{&Post{Title: "TestPost1"}, &Post{Title: "TestPost2"}} 1314 post1 := []*Post{{Title: "TestPost1"}, {Title: "TestPost2"}}
1315 post2 := &Post{Title: "TestPost3"} 1315 post2 := &Post{Title: "TestPost3"}
1316 post3 := []interface{}{&Post{Title: "TestPost4"}} 1316 post3 := []interface{}{&Post{Title: "TestPost4"}}
1317 1317
......
...@@ -195,7 +195,7 @@ func snakeString(s string) string { ...@@ -195,7 +195,7 @@ func snakeString(s string) string {
195 } 195 }
196 data = append(data, d) 196 data = append(data, d)
197 } 197 }
198 return strings.ToLower(string(data[:len(data)])) 198 return strings.ToLower(string(data[:]))
199 } 199 }
200 200
201 // camel string, xx_yy to XxYy 201 // camel string, xx_yy to XxYy
...@@ -220,7 +220,7 @@ func camelString(s string) string { ...@@ -220,7 +220,7 @@ func camelString(s string) string {
220 } 220 }
221 data = append(data, d) 221 data = append(data, d)
222 } 222 }
223 return string(data[:len(data)]) 223 return string(data[:])
224 } 224 }
225 225
226 type argString []string 226 type argString []string
......
...@@ -88,7 +88,7 @@ func (l *logFilter) Filter(ctx *beecontext.Context) bool { ...@@ -88,7 +88,7 @@ func (l *logFilter) Filter(ctx *beecontext.Context) bool {
88 if requestPath == "/favicon.ico" || requestPath == "/robots.txt" { 88 if requestPath == "/favicon.ico" || requestPath == "/robots.txt" {
89 return true 89 return true
90 } 90 }
91 for prefix, _ := range StaticDir { 91 for prefix := range StaticDir {
92 if strings.HasPrefix(requestPath, prefix) { 92 if strings.HasPrefix(requestPath, prefix) {
93 return true 93 return true
94 } 94 }
...@@ -171,7 +171,7 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM ...@@ -171,7 +171,7 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
171 p.addToRouter(m, pattern, route) 171 p.addToRouter(m, pattern, route)
172 } 172 }
173 } else { 173 } else {
174 for k, _ := range methods { 174 for k := range methods {
175 if k == "*" { 175 if k == "*" {
176 for _, m := range HTTPMETHOD { 176 for _, m := range HTTPMETHOD {
177 p.addToRouter(m, pattern, route) 177 p.addToRouter(m, pattern, route)
...@@ -332,7 +332,7 @@ func (p *ControllerRegistor) AddMethod(method, pattern string, f FilterFunc) { ...@@ -332,7 +332,7 @@ func (p *ControllerRegistor) AddMethod(method, pattern string, f FilterFunc) {
332 methods[strings.ToUpper(method)] = strings.ToUpper(method) 332 methods[strings.ToUpper(method)] = strings.ToUpper(method)
333 } 333 }
334 route.methods = methods 334 route.methods = methods
335 for k, _ := range methods { 335 for k := range methods {
336 if k == "*" { 336 if k == "*" {
337 for _, m := range HTTPMETHOD { 337 for _, m := range HTTPMETHOD {
338 p.addToRouter(m, pattern, route) 338 p.addToRouter(m, pattern, route)
......
...@@ -179,7 +179,6 @@ func (rp *MemProvider) SessionRegenerate(oldsid, sid string) (session.SessionSto ...@@ -179,7 +179,6 @@ func (rp *MemProvider) SessionRegenerate(oldsid, sid string) (session.SessionSto
179 } else { 179 } else {
180 client.Delete(oldsid) 180 client.Delete(oldsid)
181 item.Key = sid 181 item.Key = sid
182 item.Value = item.Value
183 item.Expiration = int32(rp.maxlifetime) 182 item.Expiration = int32(rp.maxlifetime)
184 client.Set(item) 183 client.Set(item)
185 contain = item.Value 184 contain = item.Value
......
1 // Copyright 2014 beego Author. All Rights Reserved. 1 // Copyright 2014 beego Author. All Rights Reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 package session 15 package session
16 16
17 import ( 17 import (
18 "container/list" 18 "container/list"
19 "net/http" 19 "net/http"
20 "sync" 20 "sync"
21 "time" 21 "time"
22 ) 22 )
23 23
24 var mempder = &MemProvider{list: list.New(), sessions: make(map[string]*list.Element)} 24 var mempder = &MemProvider{list: list.New(), sessions: make(map[string]*list.Element)}
25 25
26 // memory session store. 26 // memory session store.
27 // it saved sessions in a map in memory. 27 // it saved sessions in a map in memory.
28 type MemSessionStore struct { 28 type MemSessionStore struct {
29 sid string //session id 29 sid string //session id
30 timeAccessed time.Time //last access time 30 timeAccessed time.Time //last access time
31 value map[interface{}]interface{} //session store 31 value map[interface{}]interface{} //session store
32 lock sync.RWMutex 32 lock sync.RWMutex
33 } 33 }
34 34
35 // set value to memory session 35 // set value to memory session
36 func (st *MemSessionStore) Set(key, value interface{}) error { 36 func (st *MemSessionStore) Set(key, value interface{}) error {
37 st.lock.Lock() 37 st.lock.Lock()
38 defer st.lock.Unlock() 38 defer st.lock.Unlock()
39 st.value[key] = value 39 st.value[key] = value
40 return nil 40 return nil
41 } 41 }
42 42
43 // get value from memory session by key 43 // get value from memory session by key
44 func (st *MemSessionStore) Get(key interface{}) interface{} { 44 func (st *MemSessionStore) Get(key interface{}) interface{} {
45 st.lock.RLock() 45 st.lock.RLock()
46 defer st.lock.RUnlock() 46 defer st.lock.RUnlock()
47 if v, ok := st.value[key]; ok { 47 if v, ok := st.value[key]; ok {
48 return v 48 return v
49 } else { 49 } else {
50 return nil 50 return nil
51 } 51 }
52 } 52 }
53 53
54 // delete in memory session by key 54 // delete in memory session by key
55 func (st *MemSessionStore) Delete(key interface{}) error { 55 func (st *MemSessionStore) Delete(key interface{}) error {
56 st.lock.Lock() 56 st.lock.Lock()
57 defer st.lock.Unlock() 57 defer st.lock.Unlock()
58 delete(st.value, key) 58 delete(st.value, key)
59 return nil 59 return nil
60 } 60 }
61 61
62 // clear all values in memory session 62 // clear all values in memory session
63 func (st *MemSessionStore) Flush() error { 63 func (st *MemSessionStore) Flush() error {
64 st.lock.Lock() 64 st.lock.Lock()
65 defer st.lock.Unlock() 65 defer st.lock.Unlock()
66 st.value = make(map[interface{}]interface{}) 66 st.value = make(map[interface{}]interface{})
67 return nil 67 return nil
68 } 68 }
69 69
70 // get this id of memory session store 70 // get this id of memory session store
71 func (st *MemSessionStore) SessionID() string { 71 func (st *MemSessionStore) SessionID() string {
72 return st.sid 72 return st.sid
73 } 73 }
74 74
75 // Implement method, no used. 75 // Implement method, no used.
76 func (st *MemSessionStore) SessionRelease(w http.ResponseWriter) { 76 func (st *MemSessionStore) SessionRelease(w http.ResponseWriter) {
77 } 77 }
78 78
79 type MemProvider struct { 79 type MemProvider struct {
80 lock sync.RWMutex // locker 80 lock sync.RWMutex // locker
81 sessions map[string]*list.Element // map in memory 81 sessions map[string]*list.Element // map in memory
82 list *list.List // for gc 82 list *list.List // for gc
83 maxlifetime int64 83 maxlifetime int64
84 savePath string 84 savePath string
85 } 85 }
86 86
87 // init memory session 87 // init memory session
88 func (pder *MemProvider) SessionInit(maxlifetime int64, savePath string) error { 88 func (pder *MemProvider) SessionInit(maxlifetime int64, savePath string) error {
89 pder.maxlifetime = maxlifetime 89 pder.maxlifetime = maxlifetime
90 pder.savePath = savePath 90 pder.savePath = savePath
91 return nil 91 return nil
92 } 92 }
93 93
94 // get memory session store by sid 94 // get memory session store by sid
95 func (pder *MemProvider) SessionRead(sid string) (SessionStore, error) { 95 func (pder *MemProvider) SessionRead(sid string) (SessionStore, error) {
96 pder.lock.RLock() 96 pder.lock.RLock()
97 if element, ok := pder.sessions[sid]; ok { 97 if element, ok := pder.sessions[sid]; ok {
98 go pder.SessionUpdate(sid) 98 go pder.SessionUpdate(sid)
99 pder.lock.RUnlock() 99 pder.lock.RUnlock()
100 return element.Value.(*MemSessionStore), nil 100 return element.Value.(*MemSessionStore), nil
101 } else { 101 } else {
102 pder.lock.RUnlock() 102 pder.lock.RUnlock()
103 pder.lock.Lock() 103 pder.lock.Lock()
104 newsess := &MemSessionStore{sid: sid, timeAccessed: time.Now(), value: make(map[interface{}]interface{})} 104 newsess := &MemSessionStore{sid: sid, timeAccessed: time.Now(), value: make(map[interface{}]interface{})}
105 element := pder.list.PushBack(newsess) 105 element := pder.list.PushBack(newsess)
106 pder.sessions[sid] = element 106 pder.sessions[sid] = element
107 pder.lock.Unlock() 107 pder.lock.Unlock()
108 return newsess, nil 108 return newsess, nil
109 } 109 }
110 } 110 }
111 111
112 // check session store exist in memory session by sid 112 // check session store exist in memory session by sid
113 func (pder *MemProvider) SessionExist(sid string) bool { 113 func (pder *MemProvider) SessionExist(sid string) bool {
114 pder.lock.RLock() 114 pder.lock.RLock()
115 defer pder.lock.RUnlock() 115 defer pder.lock.RUnlock()
116 if _, ok := pder.sessions[sid]; ok { 116 if _, ok := pder.sessions[sid]; ok {
117 return true 117 return true
118 } else { 118 } else {
119 return false 119 return false
120 } 120 }
121 } 121 }
122 122
123 // generate new sid for session store in memory session 123 // generate new sid for session store in memory session
124 func (pder *MemProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) { 124 func (pder *MemProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
125 pder.lock.RLock() 125 pder.lock.RLock()
126 if element, ok := pder.sessions[oldsid]; ok { 126 if element, ok := pder.sessions[oldsid]; ok {
127 go pder.SessionUpdate(oldsid) 127 go pder.SessionUpdate(oldsid)
128 pder.lock.RUnlock() 128 pder.lock.RUnlock()
129 pder.lock.Lock() 129 pder.lock.Lock()
130 element.Value.(*MemSessionStore).sid = sid 130 element.Value.(*MemSessionStore).sid = sid
131 pder.sessions[sid] = element 131 pder.sessions[sid] = element
132 delete(pder.sessions, oldsid) 132 delete(pder.sessions, oldsid)
133 pder.lock.Unlock() 133 pder.lock.Unlock()
134 return element.Value.(*MemSessionStore), nil 134 return element.Value.(*MemSessionStore), nil
135 } else { 135 } else {
136 pder.lock.RUnlock() 136 pder.lock.RUnlock()
137 pder.lock.Lock() 137 pder.lock.Lock()
138 newsess := &MemSessionStore{sid: sid, timeAccessed: time.Now(), value: make(map[interface{}]interface{})} 138 newsess := &MemSessionStore{sid: sid, timeAccessed: time.Now(), value: make(map[interface{}]interface{})}
139 element := pder.list.PushBack(newsess) 139 element := pder.list.PushBack(newsess)
140 pder.sessions[sid] = element 140 pder.sessions[sid] = element
141 pder.lock.Unlock() 141 pder.lock.Unlock()
142 return newsess, nil 142 return newsess, nil
143 } 143 }
144 } 144 }
145 145
146 // delete session store in memory session by id 146 // delete session store in memory session by id
147 func (pder *MemProvider) SessionDestroy(sid string) error { 147 func (pder *MemProvider) SessionDestroy(sid string) error {
148 pder.lock.Lock() 148 pder.lock.Lock()
149 defer pder.lock.Unlock() 149 defer pder.lock.Unlock()
150 if element, ok := pder.sessions[sid]; ok { 150 if element, ok := pder.sessions[sid]; ok {
151 delete(pder.sessions, sid) 151 delete(pder.sessions, sid)
152 pder.list.Remove(element) 152 pder.list.Remove(element)
153 return nil 153 return nil
154 } 154 }
155 return nil 155 return nil
156 } 156 }
157 157
158 // clean expired session stores in memory session 158 // clean expired session stores in memory session
159 func (pder *MemProvider) SessionGC() { 159 func (pder *MemProvider) SessionGC() {
160 pder.lock.RLock() 160 pder.lock.RLock()
161 for { 161 for {
162 element := pder.list.Back() 162 element := pder.list.Back()
163 if element == nil { 163 if element == nil {
164 break 164 break
165 } 165 }
166 if (element.Value.(*MemSessionStore).timeAccessed.Unix() + pder.maxlifetime) < time.Now().Unix() { 166 if (element.Value.(*MemSessionStore).timeAccessed.Unix() + pder.maxlifetime) < time.Now().Unix() {
167 pder.lock.RUnlock() 167 pder.lock.RUnlock()
168 pder.lock.Lock() 168 pder.lock.Lock()
169 pder.list.Remove(element) 169 pder.list.Remove(element)
170 delete(pder.sessions, element.Value.(*MemSessionStore).sid) 170 delete(pder.sessions, element.Value.(*MemSessionStore).sid)
171 pder.lock.Unlock() 171 pder.lock.Unlock()
172 pder.lock.RLock() 172 pder.lock.RLock()
173 } else { 173 } else {
174 break 174 break
175 } 175 }
176 } 176 }
177 pder.lock.RUnlock() 177 pder.lock.RUnlock()
178 } 178 }
179 179
180 // get count number of memory session 180 // get count number of memory session
181 func (pder *MemProvider) SessionAll() int { 181 func (pder *MemProvider) SessionAll() int {
182 return pder.list.Len() 182 return pder.list.Len()
183 } 183 }
184 184
185 // expand time of session store by id in memory session 185 // expand time of session store by id in memory session
186 func (pder *MemProvider) SessionUpdate(sid string) error { 186 func (pder *MemProvider) SessionUpdate(sid string) error {
187 pder.lock.Lock() 187 pder.lock.Lock()
188 defer pder.lock.Unlock() 188 defer pder.lock.Unlock()
189 if element, ok := pder.sessions[sid]; ok { 189 if element, ok := pder.sessions[sid]; ok {
190 element.Value.(*MemSessionStore).timeAccessed = time.Now() 190 element.Value.(*MemSessionStore).timeAccessed = time.Now()
191 pder.list.MoveToFront(element) 191 pder.list.MoveToFront(element)
192 return nil 192 return nil
193 } 193 }
194 return nil 194 return nil
195 } 195 }
196 196
197 func init() { 197 func init() {
198 Register("memory", mempder) 198 Register("memory", mempder)
199 } 199 }
......
...@@ -292,7 +292,7 @@ func qpEscape(dest []byte, c byte) { ...@@ -292,7 +292,7 @@ func qpEscape(dest []byte, c byte) {
292 const nums = "0123456789ABCDEF" 292 const nums = "0123456789ABCDEF"
293 dest[0] = '=' 293 dest[0] = '='
294 dest[1] = nums[(c&0xf0)>>4] 294 dest[1] = nums[(c&0xf0)>>4]
295 dest[2] = nums[(c&0xf)] 295 dest[2] = nums[(c & 0xf)]
296 } 296 }
297 297
298 // headerToBytes enumerates the key and values in the header, and writes the results to the IO Writer 298 // headerToBytes enumerates the key and values in the header, and writes the results to the IO Writer
......
...@@ -92,18 +92,18 @@ func (p *Paginator) Pages() []int { ...@@ -92,18 +92,18 @@ func (p *Paginator) Pages() []int {
92 case page >= pageNums-4 && pageNums > 9: 92 case page >= pageNums-4 && pageNums > 9:
93 start := pageNums - 9 + 1 93 start := pageNums - 9 + 1
94 pages = make([]int, 9) 94 pages = make([]int, 9)
95 for i, _ := range pages { 95 for i := range pages {
96 pages[i] = start + i 96 pages[i] = start + i
97 } 97 }
98 case page >= 5 && pageNums > 9: 98 case page >= 5 && pageNums > 9:
99 start := page - 5 + 1 99 start := page - 5 + 1
100 pages = make([]int, int(math.Min(9, float64(page+4+1)))) 100 pages = make([]int, int(math.Min(9, float64(page+4+1))))
101 for i, _ := range pages { 101 for i := range pages {
102 pages[i] = start + i 102 pages[i] = start + i
103 } 103 }
104 default: 104 default:
105 pages = make([]int, int(math.Min(9, float64(pageNums)))) 105 pages = make([]int, int(math.Min(9, float64(pageNums))))
106 for i, _ := range pages { 106 for i := range pages {
107 pages[i] = i + 1 107 pages[i] = i + 1
108 } 108 }
109 } 109 }
......
...@@ -51,7 +51,7 @@ func SliceRandList(min, max int) []int { ...@@ -51,7 +51,7 @@ func SliceRandList(min, max int) []int {
51 t0 := time.Now() 51 t0 := time.Now()
52 rand.Seed(int64(t0.Nanosecond())) 52 rand.Seed(int64(t0.Nanosecond()))
53 list := rand.Perm(length) 53 list := rand.Perm(length)
54 for index, _ := range list { 54 for index := range list {
55 list[index] += min 55 list[index] += min
56 } 56 }
57 return list 57 return list
......
...@@ -137,7 +137,7 @@ func getRegFuncs(tag, key string) (vfs []ValidFunc, str string, err error) { ...@@ -137,7 +137,7 @@ func getRegFuncs(tag, key string) (vfs []ValidFunc, str string, err error) {
137 if err != nil { 137 if err != nil {
138 return 138 return
139 } 139 }
140 vfs = []ValidFunc{ValidFunc{"Match", []interface{}{reg, key + ".Match"}}} 140 vfs = []ValidFunc{{"Match", []interface{}{reg, key + ".Match"}}}
141 str = strings.TrimSpace(tag[:index]) + strings.TrimSpace(tag[end+len("/)"):]) 141 str = strings.TrimSpace(tag[:index]) + strings.TrimSpace(tag[end+len("/)"):])
142 return 142 return
143 } 143 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!