add return parameter name for function Cas of gtype;improve Response.Redirect* functions by adding optional parameter code

This commit is contained in:
John 2020-01-16 21:04:28 +08:00
parent 872d674182
commit b867b2a0bc
15 changed files with 46 additions and 40 deletions

View File

@ -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()
}

View File

@ -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

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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))
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)

View File

@ -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 {

View File

@ -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 <code> 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 <code> 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.

View File

@ -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.