Migrated repository
Go to file
2019-08-10 17:59:34 +08:00
.example improve gjson 2019-08-09 20:33:21 +08:00
.gitee issue template updates 2019-03-02 00:02:05 +08:00
.github README updates 2019-06-02 13:32:03 +08:00
container add package gdebug; improve gconv/gfile/gset 2019-08-09 20:05:36 +08:00
crypto update project structure 2019-07-29 21:01:19 +08:00
database add Master/Slave functions for gdb 2019-08-08 17:43:21 +08:00
debug/gdebug add package gdebug; improve gconv/gfile/gset 2019-08-09 20:05:36 +08:00
encoding improve gcfg: it returns all values if pattern is '.'; add more functions for gcfg 2019-08-07 16:58:01 +08:00
errors/gerror improve gconv to support struct field of interface{} conversion 2019-08-01 19:53:28 +08:00
frame update project structure 2019-07-29 21:01:19 +08:00
internal add package gdebug; improve gconv/gfile/gset 2019-08-09 20:05:36 +08:00
net improve status checks if there're middlewares registed for ghttp.Server 2019-08-10 17:46:11 +08:00
os add package gdebug; improve gconv/gfile/gset 2019-08-09 20:05:36 +08:00
test/gtest add package gdebug; improve gconv/gfile/gset 2019-08-09 20:05:36 +08:00
text update project structure 2019-07-29 21:01:19 +08:00
util fix issue for unit test case of gutil 2019-08-10 17:59:34 +08:00
.gitattributes dev 2017-07-04 18:31:39 +08:00
.gitignore remove thirdparty diectory, add go.mod for package dependences 2019-08-01 14:07:25 +08:00
.travis.yml improve gyaml with gopkg.in/yaml.v3; update unit test cases for gfile/gyaml/gtime/garray 2019-08-01 17:12:58 +08:00
DONATOR.MD improve unit test cases for gdb; add more functions for gjson; improve ghttp.Response.WriteStatus function 2019-07-17 23:24:27 +08:00
go.mod improve gyaml with gopkg.in/yaml.v3; update unit test cases for gfile/gyaml/gtime/garray 2019-08-01 17:12:58 +08:00
go.sum update unit test cases for garray/gparser 2019-08-01 17:31:48 +08:00
LICENSE remove reuseport in ghttp 2019-02-14 15:43:31 +08:00
README_ZH.MD update project structure 2019-07-29 21:01:19 +08:00
README.MD update project structure 2019-07-29 21:01:19 +08:00
RELEASE.MD update project structure 2019-07-29 21:01:19 +08:00
TODO.MD improve default instance name handling 2019-07-28 17:37:13 +08:00
version.go improve ghttp for session storage with redis server 2019-07-28 13:10:34 +08:00

GoFrame

Go Doc Build Status Go Report Code Coverage Production Ready License

GF(GoFrame) is a modular, full-featured and production-ready application development framework of golang. Providing a series of core components and dozens of practical modules, such as: memcache, configure, validator, logging, array/queue/set/map containers, timer/timing tasks, file/memory lock, object pool, database ORM, etc. Supporting web server integrated with router, cookie, session, logger, template, https, hooks, rewrites and many more features.

Installation

go get -u github.com/gogf/gf

or use go.mod:

require github.com/gogf/gf latest

Limitation

golang version >= 1.10

Documentation

Architecture

Quick Start

Hello World

package main

import (
    "github.com/gogf/gf/frame/g"
    "github.com/gogf/gf/net/ghttp"
)

func main() {
    s := g.Server()
    s.BindHandler("/", func(r *ghttp.Request) {
        r.Response.Write("Hello World")
    })
    s.Run()
}

Rich Router

package main

import (
    "github.com/gogf/gf/net/ghttp"
    "github.com/gogf/gf/frame/g"
)

func main() {
    s := g.Server()
    s.BindHandler("/{class}-{course}/:name/*act", func(r *ghttp.Request) {
        r.Response.Writeln(r.Get("class"))
        r.Response.Writeln(r.Get("course"))
        r.Response.Writeln(r.Get("name"))
        r.Response.Writeln(r.Get("act"))
    })
    s.SetPort(8199)
    s.Run()
}

Group Routers

package main

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

type Object struct {}

func (o *Object) Show(r *ghttp.Request) {
	r.Response.Writeln("Object Show")
}

func (o *Object) Delete(r *ghttp.Request) {
	r.Response.Writeln("Object REST Delete")
}

func Handler(r *ghttp.Request) {
	r.Response.Writeln("Handler")
}

func HookHandler(r *ghttp.Request) {
	r.Response.Writeln("Hook Handler")
}

func main() {
	s     := g.Server()
	obj   := new(Object)
	group := s.Group("/api")
	group.ALL ("*",            HookHandler, ghttp.HOOK_BEFORE_SERVE)
	group.ALL ("/handler",     Handler)
	group.ALL ("/obj",         obj)
	group.GET ("/obj/showit",  obj, "Show")
	group.REST("/obj/rest",    obj)
	s.SetPort(8199)
	s.Run()
}

or

func main() {
	s   := g.Server()
	obj := new(Object)
	s.Group("/api").Bind([]ghttp.GroupItem{
		{"ALL",  "*",            HookHandler, ghttp.HOOK_BEFORE_SERVE},
		{"ALL",  "/handler",     Handler},
		{"ALL",  "/obj",         obj},
		{"GET",  "/obj/showit",  obj, "Show"},
		{"REST", "/obj/rest",    obj},
	})
	s.SetPort(8199)
	s.Run()
}

Multi ports & domains

package main

import (
    "github.com/gogf/gf/frame/g"
    "github.com/gogf/gf/net/ghttp"
)

func Hello1(r *ghttp.Request) {
    r.Response.Write("127.0.0.1: Hello1!")
}

func Hello2(r *ghttp.Request) {
    r.Response.Write("localhost: Hello2!")
}

func main() {
    s := g.Server()
    s.Domain("127.0.0.1").BindHandler("/", Hello1)
    s.Domain("localhost").BindHandler("/", Hello2)
    s.SetPort(8100, 8200, 8300)
    s.Run()
}

Template Engine

package main

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

func main() {
	s := g.Server()
	s.BindHandler("/template", func(r *ghttp.Request) {
		r.Response.WriteTpl("index.tpl", g.Map{
			"id":   123,
			"name": "john",
		})
	})
	s.SetPort(8199)
	s.Run()
}

File Uploading

func Upload(r *ghttp.Request) {
    if f, h, e := r.FormFile("upload-file"); e == nil {
        defer f.Close()
        name   := gfile.Basename(h.Filename)
        buffer := make([]byte, h.Size)
        f.Read(buffer)
        gfile.PutBinContents("/tmp/" + name, buffer)
        r.Response.Write(name + " uploaded successly")
    } else {
        r.Response.Write(e.Error())
    }
}

ORM Operations

1. Retrieving instance

db := g.DB()
db := g.DB("user-center")

2. Chaining Operations

Where + string

// SELECT * FROM user WHERE uid>1 LIMIT 0,10
r, err := db.Table("user").Where("uid > ?", 1).Limit(0, 10).Select()

// SELECT uid,name FROM user WHERE uid>1 LIMIT 0,10
r, err := db.Table("user").Fileds("uid,name").Where("uid > ?", 1).Limit(0, 10).Select()

// SELECT * FROM user WHERE uid=1
r, err := db.Table("user").Where("u.uid=1",).One()
r, err := db.Table("user").Where("u.uid", 1).One()
r, err := db.Table("user").Where("u.uid=?", 1).One()
// SELECT * FROM user WHERE (uid=1) AND (name='john')
r, err := db.Table("user").Where("uid", 1).Where("name", "john").One()
r, err := db.Table("user").Where("uid=?", 1).And("name=?", "john").One()
// SELECT * FROM user WHERE (uid=1) OR (name='john')
r, err := db.Table("user").Where("uid=?", 1).Or("name=?", "john").One()

Where + map

// SELECT * FROM user WHERE uid=1 AND name='john'
r, err := db.Table("user").Where(g.Map{"uid" : 1, "name" : "john"}).One()
// SELECT * FROM user WHERE uid=1 AND age>18
r, err := db.Table("user").Where(g.Map{"uid" : 1, "age>" : 18}).One()

Where + struct/*struct

type User struct {
    Id       int    `json:"uid"`
    UserName string `gconv:"name"`
}
// SELECT * FROM user WHERE uid =1 AND name='john'
r, err := db.Table("user").Where(User{ Id : 1, UserName : "john"}).One()
// SELECT * FROM user WHERE uid =1
r, err := db.Table("user").Where(&User{ Id : 1}).One()

3. Update & Delete

// UPDATE user SET name='john guo' WHERE name='john'
r, err := db.Table("user").Data(gdb.Map{"name" : "john guo"}).Where("name=?", "john").Update()
r, err := db.Table("user").Data("name='john guo'").Where("name=?", "john").Update()
// UPDATE user SET status=1 ORDER BY login_time asc LIMIT 10
r, err := db.Table("user").Data("status", 1).OrderBy("login_time asc").Limit(10).Update

// DELETE FROM user WHERE uid=10
r, err := db.Table("user").Where("uid=?", 10).Delete()
// DELETE FROM user ORDER BY login_time asc LIMIT 10
r, err := db.Table("user").OrderBy("login_time asc").Limit(10).Delete()

4. Insert & Replace & Save

r, err := db.Table("user").Data(g.Map{"name": "john"}).Insert()
r, err := db.Table("user").Data(g.Map{"uid": 10000, "name": "john"}).Replace()
r, err := db.Table("user").Data(g.Map{"uid": 10001, "name": "john"}).Save()

5. Transaction

if tx, err := db.Begin(); err == nil {
    r, err := tx.Save("user", g.Map{
        "uid"  :  1,
        "name" : "john",
    })
    tx.Commit()
}

6. Error Handling

func GetOrderInfo(id int) (order *Order, err error) {
    err = g.DB().Table("order").Where("id", id).Struct(&order)
    if err != nil && err == sql.ErrNoRows {
        err = nil
    }
    return
}

More Features...

License

GF is licensed under the MIT License, 100% free and open-source, forever.

Donators

We currently accept donation by Alipay/WechatPay, please note your github/gitee account in your payment bill. If you like GF, why not buy developer a cup of coffee?

Thanks

JetBrains