example&comment update

This commit is contained in:
John Guo
2021-11-01 19:46:39 +08:00
parent c0c68d1e46
commit 79a233eb78
14 changed files with 43 additions and 36 deletions

View File

@ -4,20 +4,20 @@ import (
"fmt"
"github.com/gogf/gf/v2/os/gtime"
_ "github.com/denisenkom/go-mssqldb"
//_ "github.com/denisenkom/go-mssqldb"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
type Table2 struct {
Id string `orm:"id;pr" json:"id"` //ID
Createtime gtime.Time `orm:"createtime" json:"createtime"` //创建时间
Updatetime gtime.Time `orm:"updatetime" json:"updatetime"` //更新时间
CreateTime gtime.Time `orm:"createtime" json:"createtime"` //创建时间
UpdateTime gtime.Time `orm:"updatetime" json:"updatetime"` //更新时间
}
var table2 Table2
err := g.DB().Table("table2").Where("id=?", 1).Struct(&table2)
err := g.DB().Model("table2").Where("id", 1).Scan(&table2)
if err != nil {
panic(err)
}
fmt.Println(table2.Createtime)
fmt.Println(table2.CreateTime)
}

View File

@ -16,10 +16,10 @@ func main() {
})
s.BindHookHandlerByMap(p, map[string]ghttp.HandlerFunc{
ghttp.HookBeforeServe: func(r *ghttp.Request) {
glog.To(r.Response.Writer).Print("BeforeServe")
glog.To(r.Response.Writer).Print(r.Context(), "BeforeServe")
},
ghttp.HookAfterServe: func(r *ghttp.Request) {
glog.To(r.Response.Writer).Print("AfterServe")
glog.To(r.Response.Writer).Print(r.Context(), "AfterServe")
},
})
s.SetPort(8199)

View File

@ -10,7 +10,7 @@ func main() {
s1 := ghttp.GetServer("s1")
s1.SetPort(8882)
s1.BindHandler("/", func(r *ghttp.Request) {
glog.Print("s1")
glog.Print(r.Context(), "s1")
r.Response.Writeln("s1")
})
s1.Start()
@ -18,7 +18,7 @@ func main() {
s2 := ghttp.GetServer("s2")
s2.SetPort(8882)
s2.BindHandler("/", func(r *ghttp.Request) {
glog.Print("s2")
glog.Print(r.Context(), "s2")
r.Response.Writeln("s2")
})
s2.Start()

View File

@ -1,6 +1,7 @@
package main
import (
"context"
"time"
"github.com/gogf/gf/v2/os/gfsnotify"
@ -9,8 +10,11 @@ import (
)
func main() {
var (
ctx = context.Background()
)
callback, err := gfsnotify.Add("/home/john/temp", func(event *gfsnotify.Event) {
glog.Print("callback")
glog.Print(ctx, "callback")
})
if err != nil {
panic(err)
@ -19,9 +23,9 @@ func main() {
// 在此期间创建文件、目录、修改文件、删除文件
// 20秒后移除回调函数注册所有的回调都移除不再有任何打印信息输出
gtimer.SetTimeout(20*time.Second, func() {
gtimer.SetTimeout(ctx, 20*time.Second, func(ctx context.Context) {
gfsnotify.RemoveCallback(callback.Id)
glog.Print("remove callback")
glog.Print(ctx, "remove callback")
})
select {}

View File

@ -1,27 +1,32 @@
package main
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gfile"
)
// 设置日志等级
func main() {
path := "/tmp/glog"
var (
ctx = context.TODO()
path = "/tmp/glog"
)
g.Log().SetPath(path)
g.Log().SetStdoutPrint(false)
// 使用默认文件名称格式
g.Log().Print("标准文件名称格式,使用当前时间时期")
g.Log().Print(ctx, "标准文件名称格式,使用当前时间时期")
// 通过SetFile设置文件名称格式
g.Log().SetFile("stdout.log")
g.Log().Print("设置日志输出文件名称格式为同一个文件")
g.Log().Print(ctx, "设置日志输出文件名称格式为同一个文件")
// 链式操作设置文件名称格式
g.Log().File("stderr.log").Print("支持链式操作")
g.Log().File("error-{Ymd}.log").Print("文件名称支持带gtime日期格式")
g.Log().File("access-{Ymd}.log").Print("文件名称支持带gtime日期格式")
g.Log().File("stderr.log").Print(ctx, "支持链式操作")
g.Log().File("error-{Ymd}.log").Print(ctx, "文件名称支持带gtime日期格式")
g.Log().File("access-{Ymd}.log").Print(ctx, "文件名称支持带gtime日期格式")
list, err := gfile.ScanDir(path, "*")
g.Dump(err)

View File

@ -1,6 +1,7 @@
package main
import (
"context"
"fmt"
"time"
@ -38,7 +39,7 @@ func main() {
fmt.Println(t.UTC().String())
fmt.Println(t.In(cstLocal).String())
} else {
glog.Error(s, err)
glog.Error(context.Background(), s, err)
}
fmt.Println()
}

View File

@ -333,8 +333,10 @@ func (r *Request) GetMultipartFiles(name string) []*multipart.FileHeader {
return v
}
// Support "name[0]","name[1]","name[2]", etc. as array parameter.
key := ""
files := make([]*multipart.FileHeader, 0)
var (
key = ""
files = make([]*multipart.FileHeader, 0)
)
for i := 0; ; i++ {
key = fmt.Sprintf(`%s[%d]`, name, i)
if v := form.File[key]; len(v) > 0 {

View File

@ -102,7 +102,7 @@ func (r *Request) doGetFormStruct(pointer interface{}, mapping ...map[string]str
if data == nil {
data = map[string]interface{}{}
}
if err := r.mergeDefaultStructValue(data, pointer); err != nil {
if err = r.mergeDefaultStructValue(data, pointer); err != nil {
return data, nil
}
return data, gconv.Struct(data, pointer, mapping...)

View File

@ -142,7 +142,7 @@ func (r *Request) doGetQueryStruct(pointer interface{}, mapping ...map[string]st
if data == nil {
data = map[string]interface{}{}
}
if err := r.mergeDefaultStructValue(data, pointer); err != nil {
if err = r.mergeDefaultStructValue(data, pointer); err != nil {
return data, nil
}
return data, gconv.Struct(data, pointer, mapping...)

View File

@ -171,7 +171,7 @@ func (r *Request) doGetRequestStruct(pointer interface{}, mapping ...map[string]
if data == nil {
data = map[string]interface{}{}
}
if err := r.mergeDefaultStructValue(data, pointer); err != nil {
if err = r.mergeDefaultStructValue(data, pointer); err != nil {
return data, nil
}
return data, gconv.Struct(data, pointer, mapping...)

View File

@ -25,7 +25,7 @@ var (
// to allow it modified by developer if necessary.
Separator = string(filepath.Separator)
// DefaultPerm is the default perm for file opening.
// DefaultPermOpen is the default perm for file opening.
DefaultPermOpen = os.FileMode(0666)
// DefaultPermCopy is the default perm for file/folder copy.
@ -169,12 +169,6 @@ func IsFile(path string) bool {
return !s.IsDir()
}
// Alias of Stat.
// See Stat.
func Info(path string) (os.FileInfo, error) {
return Stat(path)
}
// Stat returns a FileInfo describing the named file.
// If there is an error, it will be of type *PathError.
func Stat(path string) (os.FileInfo, error) {
@ -273,6 +267,7 @@ func IsWritable(path string) bool {
return result
}
// Chmod is alias of os.Chmod.
// See os.Chmod.
func Chmod(path string, mode os.FileMode) error {
return os.Chmod(path, mode)
@ -401,7 +396,7 @@ func ExtName(path string) string {
}
// TempDir retrieves and returns the temporary directory of current system.
// It return "/tmp" is current in *nix system, or else it returns os.TempDir().
// It returns "/tmp" is current in *nix system, or else it returns os.TempDir().
//
// The optional parameter `names` specifies the its sub-folders/sub-files,
// which will be joined with current system separator and returned with the path.

View File

@ -18,7 +18,7 @@ import (
)
// Home returns absolute path of current user's home directory.
// The optional parameter `names` specifies the its sub-folders/sub-files,
// The optional parameter `names` specifies the sub-folders/sub-files,
// which will be joined with current system separator and returned with the path.
func Home(names ...string) (string, error) {
path, err := getHomePath()

View File

@ -74,7 +74,7 @@ func StrToSize(sizeStr string) int64 {
return -1
}
// FormatSize formats size `raw` for more human readable.
// FormatSize formats size `raw` for more manually readable.
func FormatSize(raw int64) string {
var r float64 = float64(raw)
var t float64 = 1024

View File

@ -39,7 +39,7 @@ func IsMatchString(pattern string, src string) bool {
return IsMatch(pattern, []byte(src))
}
// MatchString return bytes slice that matched `pattern`.
// Match return bytes slice that matched `pattern`.
func Match(pattern string, src []byte) ([][]byte, error) {
if r, err := getRegexp(pattern); err == nil {
return r.FindSubmatch(src), nil
@ -75,7 +75,7 @@ func MatchAllString(pattern string, src string) ([][]string, error) {
}
}
// ReplaceString replace all matched `pattern` in bytes `src` with bytes `replace`.
// Replace replace all matched `pattern` in bytes `src` with bytes `replace`.
func Replace(pattern string, replace, src []byte) ([]byte, error) {
if r, err := getRegexp(pattern); err == nil {
return r.ReplaceAll(src, replace), nil