diff --git a/.example/other/test.go b/.example/other/test.go index f4b966b39..fba507872 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -2,23 +2,10 @@ package main import ( "fmt" - "github.com/gogf/gf/os/gres" + "github.com/gogf/gf/frame/g" ) func main() { - //buffer := bytes.NewBuffer(nil) - //buffer.WriteString("\x00") - //hex.Decode() - //if v, e := strconv.ParseInt(s[2:], 16, 64); e == nil { - // return v - //} - //s := "\x00" - //fmt.Println([]byte(s)) - //return - err := gres.PackToGoFile( - "/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf-cli/public", - "/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/config.go", - "main", - ) - fmt.Println(err) + fmt.Println(g.Cfg().FilePath()) + g.Cfg().Dump() } diff --git a/container/gtype/bool.go b/container/gtype/bool.go index e090283e0..33edaa57f 100644 --- a/container/gtype/bool.go +++ b/container/gtype/bool.go @@ -57,7 +57,7 @@ func (v *Bool) Val() bool { } // Cas executes the compare-and-swap operation for value. -func (v *Bool) Cas(old, new bool) bool { +func (v *Bool) Cas(old, new bool) (swapped bool) { var oldInt32, newInt32 int32 if old { oldInt32 = 1 diff --git a/container/gtype/byte.go b/container/gtype/byte.go index 02a5c36f9..88f1fdcc1 100644 --- a/container/gtype/byte.go +++ b/container/gtype/byte.go @@ -49,7 +49,7 @@ func (v *Byte) Add(delta byte) (new byte) { } // Cas executes the compare-and-swap operation for value. -func (v *Byte) Cas(old, new byte) bool { +func (v *Byte) Cas(old, new byte) (swapped bool) { return atomic.CompareAndSwapInt32(&v.value, int32(old), int32(new)) } diff --git a/container/gtype/float32.go b/container/gtype/float32.go index 4334ac3b3..11e834c11 100644 --- a/container/gtype/float32.go +++ b/container/gtype/float32.go @@ -62,7 +62,7 @@ func (v *Float32) Add(delta float32) (new float32) { } // Cas executes the compare-and-swap operation for value. -func (v *Float32) Cas(old, new float32) bool { +func (v *Float32) Cas(old, new float32) (swapped bool) { return atomic.CompareAndSwapUint32(&v.value, math.Float32bits(old), math.Float32bits(new)) } diff --git a/container/gtype/float64.go b/container/gtype/float64.go index f3eaf9cdc..4c01f4208 100644 --- a/container/gtype/float64.go +++ b/container/gtype/float64.go @@ -62,7 +62,7 @@ func (v *Float64) Add(delta float64) (new float64) { } // Cas executes the compare-and-swap operation for value. -func (v *Float64) Cas(old, new float64) bool { +func (v *Float64) Cas(old, new float64) (swapped bool) { return atomic.CompareAndSwapUint64(&v.value, math.Float64bits(old), math.Float64bits(new)) } diff --git a/container/gtype/int.go b/container/gtype/int.go index e5e6a4210..cf2ee6157 100644 --- a/container/gtype/int.go +++ b/container/gtype/int.go @@ -49,7 +49,7 @@ func (v *Int) Add(delta int) (new int) { } // Cas executes the compare-and-swap operation for value. -func (v *Int) Cas(old, new int) bool { +func (v *Int) Cas(old, new int) (swapped bool) { return atomic.CompareAndSwapInt64(&v.value, int64(old), int64(new)) } diff --git a/container/gtype/int32.go b/container/gtype/int32.go index 148c55030..4131184d5 100644 --- a/container/gtype/int32.go +++ b/container/gtype/int32.go @@ -49,7 +49,7 @@ func (v *Int32) Add(delta int32) (new int32) { } // Cas executes the compare-and-swap operation for value. -func (v *Int32) Cas(old, new int32) bool { +func (v *Int32) Cas(old, new int32) (swapped bool) { return atomic.CompareAndSwapInt32(&v.value, old, new) } diff --git a/container/gtype/int64.go b/container/gtype/int64.go index 678a7fdc5..484d948bf 100644 --- a/container/gtype/int64.go +++ b/container/gtype/int64.go @@ -49,7 +49,7 @@ func (v *Int64) Add(delta int64) (new int64) { } // Cas executes the compare-and-swap operation for value. -func (v *Int64) Cas(old, new int64) bool { +func (v *Int64) Cas(old, new int64) (swapped bool) { return atomic.CompareAndSwapInt64(&v.value, old, new) } diff --git a/container/gtype/uint.go b/container/gtype/uint.go index 2d9e29778..bb50ea6c8 100644 --- a/container/gtype/uint.go +++ b/container/gtype/uint.go @@ -49,7 +49,7 @@ func (v *Uint) Add(delta uint) (new uint) { } // Cas executes the compare-and-swap operation for value. -func (v *Uint) Cas(old, new uint) bool { +func (v *Uint) Cas(old, new uint) (swapped bool) { return atomic.CompareAndSwapUint64(&v.value, uint64(old), uint64(new)) } diff --git a/container/gtype/uint32.go b/container/gtype/uint32.go index 589557741..5ef5bf2d6 100644 --- a/container/gtype/uint32.go +++ b/container/gtype/uint32.go @@ -49,7 +49,7 @@ func (v *Uint32) Add(delta uint32) (new uint32) { } // Cas executes the compare-and-swap operation for value. -func (v *Uint32) Cas(old, new uint32) bool { +func (v *Uint32) Cas(old, new uint32) (swapped bool) { return atomic.CompareAndSwapUint32(&v.value, old, new) } diff --git a/container/gtype/uint64.go b/container/gtype/uint64.go index 8e9ddc83a..cede4aa6e 100644 --- a/container/gtype/uint64.go +++ b/container/gtype/uint64.go @@ -49,7 +49,7 @@ func (v *Uint64) Add(delta uint64) (new uint64) { } // Cas executes the compare-and-swap operation for value. -func (v *Uint64) Cas(old, new uint64) bool { +func (v *Uint64) Cas(old, new uint64) (swapped bool) { return atomic.CompareAndSwapUint64(&v.value, old, new) } diff --git a/container/gtype/z_bench_basic_test.go b/container/gtype/z_bench_basic_test.go index c82185d0b..67c6ce3ca 100644 --- a/container/gtype/z_bench_basic_test.go +++ b/container/gtype/z_bench_basic_test.go @@ -49,6 +49,12 @@ func BenchmarkInt_Add(b *testing.B) { } } +func BenchmarkInt_Cas(b *testing.B) { + for i := 0; i < b.N; i++ { + it.Cas(i, i) + } +} + func BenchmarkInt32_Set(b *testing.B) { for i := int32(0); i < int32(b.N); i++ { it32.Set(i) diff --git a/encoding/gcompress/gcompress_zip_file.go b/encoding/gcompress/gcompress_zip_file.go index 6b78421b6..595138d2d 100644 --- a/encoding/gcompress/gcompress_zip_file.go +++ b/encoding/gcompress/gcompress_zip_file.go @@ -72,11 +72,11 @@ func doZipPathWriter(path string, zipWriter *zip.Writer, prefix ...string) error headerPrefix = prefix[0] } headerPrefix = strings.TrimRight(headerPrefix, "\\/") - if gfile.IsDir(path) { - if len(headerPrefix) > 0 { - headerPrefix += "/" - } - headerPrefix = headerPrefix + gfile.Basename(path) + if len(headerPrefix) > 0 && gfile.IsDir(path) { + headerPrefix += "/" + } + if headerPrefix == "" { + headerPrefix = gfile.Basename(path) } headerPrefix = strings.Replace(headerPrefix, "//", "/", -1) for _, file := range files { diff --git a/net/ghttp/ghttp_response.go b/net/ghttp/ghttp_response.go index 741728c27..8ada55ea7 100644 --- a/net/ghttp/ghttp_response.go +++ b/net/ghttp/ghttp_response.go @@ -90,16 +90,24 @@ func (r *Response) ServeFileDownload(path string, name ...string) { r.Server.serveFile(r.Request, serveFile) } -// RedirectTo redirects client to another location using http status 302. -func (r *Response) RedirectTo(location string) { +// RedirectTo redirects client to another location. +// The optional parameter specifies the http status code for redirecting, +// which commonly can be 301 or 302. It's 302 in default. +func (r *Response) RedirectTo(location string, code ...int) { r.Header().Set("Location", location) - r.WriteHeader(http.StatusFound) + if len(code) > 0 { + r.WriteHeader(code[0]) + } else { + r.WriteHeader(http.StatusFound) + } r.Request.Exit() } -// RedirectBack redirects client back to referer using http status 302. -func (r *Response) RedirectBack() { - r.RedirectTo(r.Request.GetReferer()) +// RedirectBack redirects client back to referer. +// The optional parameter specifies the http status code for redirecting, +// which commonly can be 301 or 302. It's 302 in default. +func (r *Response) RedirectBack(code ...int) { + r.RedirectTo(r.Request.GetReferer(), code...) } // BufferString returns the buffered content as []byte. diff --git a/net/ghttp/ghttp_server_config_static.go b/net/ghttp/ghttp_server_config_static.go index aad01bdb8..36c482b49 100644 --- a/net/ghttp/ghttp_server_config_static.go +++ b/net/ghttp/ghttp_server_config_static.go @@ -26,9 +26,14 @@ type staticPathItem struct { path string // The static path. } -// SetIndexFiles sets the index files fro server. -func (s *Server) SetIndexFiles(index []string) { - s.config.IndexFiles = index +// SetIndexFiles sets the index files for server. +func (s *Server) SetIndexFiles(indexFiles []string) { + s.config.IndexFiles = indexFiles +} + +// GetIndexFiles retrieves and returns the index files from server. +func (s *Server) GetIndexFiles() []string { + return s.config.IndexFiles } // SetIndexFolder enables/disables listing the sub-files if requesting a directory.