gf

package module
v2.0.0-...-cbac34e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2019 License: MIT Imports: 0 Imported by: 0

README

GoFrame

Go Doc Build Status Go Report Code Coverage Production Ready License

English | 简体中文

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, middleware, logger, template, https, hooks, rewrites and many more features.

Installation

go get -u github.com/gogf/gf

suggested using 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()
}

Router & Middleware

package main

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

func main() {
    s := g.Server()
    s.Group("/api.v2", func(g *ghttp.RouterGroup) {
        g.Middleware(func(r *ghttp.Request) {
            r.Response.Write("start")
            r.Middleware.Next()
            r.Response.Write("end")
        })
        g.Group("/order", func(g *ghttp.RouterGroup) {
            g.GET("/list", func(r *ghttp.Request) {
                r.Response.Write("list")
            })
        })
        g.Group("/user", func(g *ghttp.RouterGroup) {
            g.GET("/info", func(r *ghttp.Request) {
                r.Response.Write("info")
            })
            g.POST("/edit", func(r *ghttp.Request) {
                r.Response.Write("edit")
            })
        })
        g.Group("/hook", func(g *ghttp.RouterGroup) {
            g.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
                r.Response.Write("hook any")
            })
            g.Hook("/:name", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
                r.Response.Write("hook name")
            })
        })
    })
    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.PutBytes("/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

Documentation

Index

Constants

View Source
const AUTHORS = "john<john@goframe.org>"
View Source
const VERSION = "v2.0.0"

Variables

This section is empty.

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
container
garray
Package garray provides concurrent-safe/unsafe arrays.
Package garray provides concurrent-safe/unsafe arrays.
gchan
Package gchan provides graceful channel for no panic operations.
Package gchan provides graceful channel for no panic operations.
glist
Package glist provides a concurrent-safe/unsafe doubly linked list.
Package glist provides a concurrent-safe/unsafe doubly linked list.
gmap
Package gmap provides concurrent-safe/unsafe map containers.
Package gmap provides concurrent-safe/unsafe map containers.
gpool
Package gpool provides object-reusable concurrent-safe pool.
Package gpool provides object-reusable concurrent-safe pool.
gqueue
Package gqueue provides a dynamic/static concurrent-safe queue.
Package gqueue provides a dynamic/static concurrent-safe queue.
gring
Package gring provides a concurrent-safe/unsafe ring(circular lists).
Package gring provides a concurrent-safe/unsafe ring(circular lists).
gset
Package gset provides kinds of concurrent-safe/unsafe sets.
Package gset provides kinds of concurrent-safe/unsafe sets.
gtree
Package gtree provides concurrent-safe/unsafe tree containers.
Package gtree provides concurrent-safe/unsafe tree containers.
gtype
Package gtype provides kinds of high performance and concurrent-safe basic variable types.
Package gtype provides kinds of high performance and concurrent-safe basic variable types.
gvar
Package gvar provides an universal variable type, like generics.
Package gvar provides an universal variable type, like generics.
crypto
gaes
Package gaes provides useful API for AES encryption/decryption algorithms.
Package gaes provides useful API for AES encryption/decryption algorithms.
gcrc32
Package gcrc32 provides useful API for CRC32 encryption algorithms.
Package gcrc32 provides useful API for CRC32 encryption algorithms.
gdes
Package gdes provides useful API for DES encryption/decryption algorithms.
Package gdes provides useful API for DES encryption/decryption algorithms.
gmd5
Package gmd5 provides useful API for MD5 encryption algorithms.
Package gmd5 provides useful API for MD5 encryption algorithms.
gsha1
Package gsha1 provides useful API for SHA1 encryption algorithms.
Package gsha1 provides useful API for SHA1 encryption algorithms.
database
gdb
Package gdb provides ORM features for popular relationship databases.
Package gdb provides ORM features for popular relationship databases.
gkvdb
Package gkvdb provides a lightweight, embeddable and persistent key-value database.
Package gkvdb provides a lightweight, embeddable and persistent key-value database.
gredis
Package gredis provides convenient client for redis server.
Package gredis provides convenient client for redis server.
debug
gdebug
Package gdebug contains facilities for programs to debug themselves while they are running.
Package gdebug contains facilities for programs to debug themselves while they are running.
encoding
gbase64
Package gbase64 provides useful API for BASE64 encoding/decoding algorithm.
Package gbase64 provides useful API for BASE64 encoding/decoding algorithm.
gbinary
Package gbinary provides useful API for handling binary/bytes data.
Package gbinary provides useful API for handling binary/bytes data.
gcharset
Package charset implements character-set conversion functionality.
Package charset implements character-set conversion functionality.
gcompress
Package gcompress provides kinds of compression algorithms for binary/bytes data.
Package gcompress provides kinds of compression algorithms for binary/bytes data.
ghash
Package ghash provides some popular hash functions(uint32/uint64) in go.
Package ghash provides some popular hash functions(uint32/uint64) in go.
ghtml
Package ghtml provides useful API for HTML content handling.
Package ghtml provides useful API for HTML content handling.
gini
Package gini provides accessing and converting for INI content.
Package gini provides accessing and converting for INI content.
gjson
Package gjson provides convenient API for JSON/XML/YAML/TOML data handling.
Package gjson provides convenient API for JSON/XML/YAML/TOML data handling.
gparser
Package gparser provides convenient API for accessing/converting variable and JSON/XML/YAML/TOML.
Package gparser provides convenient API for accessing/converting variable and JSON/XML/YAML/TOML.
gtoml
Package gtoml provides accessing and converting for TOML content.
Package gtoml provides accessing and converting for TOML content.
gurl
Package gurl provides useful API for URL handling.
Package gurl provides useful API for URL handling.
gxml
Package gxml provides accessing and converting for XML content.
Package gxml provides accessing and converting for XML content.
gyaml
Package gyaml provides accessing and converting for YAML content.
Package gyaml provides accessing and converting for YAML content.
errors
gerror
Package errors provides simple functions to manipulate errors.
Package errors provides simple functions to manipulate errors.
frame
g
gins
Package gins provides instances management and core components management.
Package gins provides instances management and core components management.
gmvc
Package gmvc provides basic object classes for MVC.
Package gmvc provides basic object classes for MVC.
i18n
gi18n
Package gi18n implements internationalization and localization.
Package gi18n implements internationalization and localization.
internal
cmdenv
Package cmdenv provides access to certain variable for both command options and environment.
Package cmdenv provides access to certain variable for both command options and environment.
empty
Package empty provides checks for empty variables.
Package empty provides checks for empty variables.
fileinfo
Package fileinfo provides virtual os.FileInfo for given information.
Package fileinfo provides virtual os.FileInfo for given information.
mutex
Package mutex provides switch of concurrent safe feature for sync.Mutex.
Package mutex provides switch of concurrent safe feature for sync.Mutex.
rwmutex
Package rwmutex provides switch of concurrent safe feature for sync.RWMutex.
Package rwmutex provides switch of concurrent safe feature for sync.RWMutex.
structs
Package structs provides functions for struct conversion.
Package structs provides functions for struct conversion.
utilbytes
Package utibytes provides some bytes functions for internal usage.
Package utibytes provides some bytes functions for internal usage.
utilstr
Package utilstr provides some string functions for internal usage.
Package utilstr provides some string functions for internal usage.
net
ghttp
Package ghttp provides powerful http server and simple client implements.
Package ghttp provides powerful http server and simple client implements.
gipv4
Package gipv4 provides useful API for IPv4 address handling.
Package gipv4 provides useful API for IPv4 address handling.
gipv6
Package gipv4 provides useful API for IPv6 address handling.
Package gipv4 provides useful API for IPv6 address handling.
gsmtp
Package gsmtp provides a SMTP client to access remote mail server.
Package gsmtp provides a SMTP client to access remote mail server.
gtcp
Package gtcp provides TCP server and client implementations.
Package gtcp provides TCP server and client implementations.
gudp
Package gtcp provides UDP server and client implementations.
Package gtcp provides UDP server and client implementations.
os
gcache
Package gcache provides high performance and concurrent-safe in-memory cache for process.
Package gcache provides high performance and concurrent-safe in-memory cache for process.
gcfg
Package gcfg provides reading, caching and managing for configuration.
Package gcfg provides reading, caching and managing for configuration.
gcmd
Package gcmd provides console operations, like options/arguments reading and command running.
Package gcmd provides console operations, like options/arguments reading and command running.
gcron
Package gcron implements a cron pattern parser and job runner.
Package gcron implements a cron pattern parser and job runner.
genv
Package genv provides operations for environment variables of system.
Package genv provides operations for environment variables of system.
gfcache
Package gfcache provides reading and caching for file contents.
Package gfcache provides reading and caching for file contents.
gfile
Package gfile provides easy-to-use operations for file system.
Package gfile provides easy-to-use operations for file system.
gflock
Package gflock implements a concurrent-safe sync.Locker interface for file locking.
Package gflock implements a concurrent-safe sync.Locker interface for file locking.
gfpool
Package gfpool provides io-reusable pool for file pointer.
Package gfpool provides io-reusable pool for file pointer.
gfsnotify
Package gfsnotify provides a platform-independent interface for file system notifications.
Package gfsnotify provides a platform-independent interface for file system notifications.
glog
Package glog implements powerful and easy-to-use levelled logging functionality.
Package glog implements powerful and easy-to-use levelled logging functionality.
gmlock
Package gmlock implements a concurrent-safe memory-based locker.
Package gmlock implements a concurrent-safe memory-based locker.
gmutex
Package gmutex implements graceful concurrent-safe mutex with more rich features.
Package gmutex implements graceful concurrent-safe mutex with more rich features.
gproc
Package gproc implements management and communication for processes.
Package gproc implements management and communication for processes.
gres
Package gres provides resource management and packing/unpacking feature between files and bytes.
Package gres provides resource management and packing/unpacking feature between files and bytes.
grpool
Package grpool implements a goroutine reusable pool.
Package grpool implements a goroutine reusable pool.
gspath
Package gspath implements file index and search for folders.
Package gspath implements file index and search for folders.
gtime
Package gtime provides functionality for measuring and displaying time.
Package gtime provides functionality for measuring and displaying time.
gtimer
Package gtimer implements Hierarchical Timing Wheel for interval/delayed jobs running and management.
Package gtimer implements Hierarchical Timing Wheel for interval/delayed jobs running and management.
gview
Package gview implements a template engine based on text/template.
Package gview implements a template engine based on text/template.
test
gtest
Package gtest provides convenient test utilities for unit testing.
Package gtest provides convenient test utilities for unit testing.
text
gregex
Package gregex provides high performance API for regular expression functionality.
Package gregex provides high performance API for regular expression functionality.
gstr
Package gstr provides functions for string handling.
Package gstr provides functions for string handling.
util
gconv
Package gconv implements powerful and easy-to-use converting functionality for any types of variables.
Package gconv implements powerful and easy-to-use converting functionality for any types of variables.
gpage
Package gpage provides useful paging functionality for web pages.
Package gpage provides useful paging functionality for web pages.
grand
Package grand provides high performance random string generation functionality.
Package grand provides high performance random string generation functionality.
gutil
Package gutil provides utility functions.
Package gutil provides utility functions.
gvalid
Package gvalid implements powerful and useful data/form validation functionality.
Package gvalid implements powerful and useful data/form validation functionality.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL