beego is used for rapid development of RESTful APIs, web apps and backend services in Go. It is inspired by Tornado, Sinatra and Flask. beego has some Go-specific features such as interfaces and struct embedding.
More info at beego.me.
Please see Documentation for more.
mkdir hello
cd hello
go mod init
go get github.com/astaxie/beego
package main
import "github.com/astaxie/beego/server/web"
func main(){
web.Run()
}
go build hello.go
./hello
Go to http://localhost:8080
Congratulations! You've just built your first beego app.
package main
import (
"github.com/astaxie/beego/client/orm"
"github.com/astaxie/beego/core/logs"
_ "github.com/go-sql-driver/mysql"
)
// User -
type User struct {
ID int `orm:"column(id)"`
Name string `orm:"column(name)"`
}
func init() {
// need to register models in init
orm.RegisterModel(new(User))
// need to register db driver
orm.RegisterDriver("mysql", orm.DRMySQL)
// need to register default database
orm.RegisterDataBase("default", "mysql", "beego:test@tcp(192.168.0.105:13306)/orm_test?charset=utf8")
}
func main() {
// automatically build table
orm.RunSyncdb("default", false, true)
// create orm object, and it will use `default` database
o := orm.NewOrm()
// data
user := new(User)
user.Name = "mike"
// insert data
id, err := o.Insert(user)
if err != nil {
logs.Info(err)
}
// ...
}
package main
import (
"github.com/astaxie/beego/client/httplib"
"github.com/astaxie/beego/core/logs"
)
func main() {
// Get, more methods please read docs
req := httplib.Get("http://beego.me/")
str, err := req.String()
if err != nil {
logs.Error(err)
}
logs.Info(str)
}
package main
import (
"context"
"github.com/astaxie/beego/core/config"
"github.com/astaxie/beego/core/logs"
)
var (
ConfigFile = "./app.conf"
)
func main() {
cfg, err := config.NewConfig("ini", ConfigFile)
if err != nil {
logs.Critical("An error occurred:", err)
panic(err)
}
res, _ := cfg.String(context.Background(), "name")
logs.Info("load config name is", res)
}
package main
import (
"github.com/astaxie/beego/core/logs"
)
func main() {
err := logs.SetLogger(logs.AdapterFile, `{"filename":"project.log","level":7,"maxlines":0,"maxsize":0,"daily":true,"maxdays":10,"color":true}`)
if err != nil {
panic(err)
}
logs.Info("hello beego")
}
package main
import (
"context"
"time"
"github.com/astaxie/beego/core/logs"
"github.com/astaxie/beego/task"
)
func main() {
// create a task
tk1 := task.NewTask("tk1", "0/3 * * * * *", func(ctx context.Context) error { logs.Info("tk1"); return nil })
// check task
err := tk1.Run(context.Background())
if err != nil {
logs.Error(err)
}
// add task to global todolist
task.AddTask("tk1", tk1)
// start tasks
task.StartTask()
// wait 12 second
time.Sleep(12 * time.Second)
defer task.StopTask()
}
package main
import (
"context"
"time"
"github.com/astaxie/beego/client/cache"
// don't forget this
_ "github.com/astaxie/beego/client/cache/redis"
"github.com/astaxie/beego/core/logs"
)
func main() {
// create cache
bm, err := cache.NewCache("redis", `{"key":"default", "conn":":6379", "password":"123456", "dbNum":"0"}`)
if err != nil {
logs.Error(err)
}
// put
isPut := bm.Put(context.Background(), "astaxie", 1, time.Second*10)
logs.Info(isPut)
isPut = bm.Put(context.Background(), "hello", "world", time.Second*10)
logs.Info(isPut)
// get
result, _ := bm.Get(context.Background(),"astaxie")
logs.Info(string(result.([]byte)))
multiResult, _ := bm.GetMulti(context.Background(), []string{"astaxie", "hello"})
for i := range multiResult {
logs.Info(string(multiResult[i].([]byte)))
}
// isExist
isExist, _ := bm.IsExist(context.Background(), "astaxie")
logs.Info(isExist)
// delete
isDelete := bm.Delete(context.Background(), "astaxie")
logs.Info(isDelete)
}
- RESTful support
- MVC architecture
- Modularity
- Auto API documents
- Annotation router
- Namespace
- Powerful development tools
- Full stack for Web & API
- http://beego.me/community
- Welcome to join us in Slack: https://beego.slack.com, you can get invited from here
- QQ Group Group ID:523992905
beego source code is licensed under the Apache Licence, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0.html).