像Laravel那样的Golang框架

像Laravel那样的Golang框架 GitHub - goal-web/goal: 一个优雅的 Go 框架

1 回复

更多关于像Laravel那样的Golang框架的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


对于寻找类似 Laravel 的 Go 框架,我推荐 Goal。它确实是一个优雅的 Go 框架,借鉴了 Laravel 的许多设计理念,提供了类似的开发体验。以下是一些关键特性和示例代码:

1. 路由定义

Goal 的路由系统类似于 Laravel,支持 RESTful 路由和中间件。

package main

import (
    "github.com/goal-web/goal/app"
    "github.com/goal-web/goal/routing"
)

func main() {
    application := app.Singleton()

    routing.Route("GET", "/", func() string {
        return "Hello, Goal!"
    })

    routing.Route("GET", "/user/{id}", func(id string) string {
        return "User ID: " + id
    })

    application.Run()
}

2. 中间件支持

Goal 支持中间件,类似于 Laravel 的中间件机制。

package main

import (
    "github.com/goal-web/goal/app"
    "github.com/goal-web/goal/routing"
    "net/http"
)

func AuthMiddleware(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        // 验证逻辑
        if r.Header.Get("Authorization") == "" {
            http.Error(w, "Unauthorized", http.StatusUnauthorized)
            return
        }
        next.ServeHTTP(w, r)
    })
}

func main() {
    application := app.Singleton()

    routing.Route("GET", "/dashboard", func() string {
        return "Dashboard"
    }).Middleware(AuthMiddleware)

    application.Run()
}

3. 依赖注入

Goal 内置了依赖注入容器,类似于 Laravel 的服务容器。

package main

import (
    "github.com/goal-web/goal/app"
    "github.com/goal-web/goal/contracts"
)

type UserService struct {
    // 服务实现
}

func (u *UserService) GetUser(id string) string {
    return "User " + id
}

func main() {
    application := app.Singleton()

    application.Singleton("user.service", func(app contracts.Application) interface{} {
        return &UserService{}
    })

    // 在控制器中解析依赖
    application.Call(func(service *UserService) {
        println(service.GetUser("123"))
    })

    application.Run()
}

4. 数据库操作

Goal 提供了数据库 ORM 和查询构造器,类似于 Laravel 的 Eloquent。

package main

import (
    "github.com/goal-web/goal/app"
    "github.com/goal-web/goal/database"
)

type User struct {
    ID   uint   `json:"id"`
    Name string `json:"name"`
}

func main() {
    application := app.Singleton()

    // 配置数据库连接
    database.DefaultConfig("mysql", "user:password@tcp(127.0.0.1:3306)/dbname")

    // 查询示例
    var users []User
    database.Table("users").Where("active", 1).Get(&users)

    // 插入示例
    database.Table("users").Insert(map[string]interface{}{
        "name": "John Doe",
    })

    application.Run()
}

5. 模板引擎

Goal 支持模板渲染,类似于 Laravel 的 Blade 模板。

package main

import (
    "github.com/goal-web/goal/app"
    "github.com/goal-web/goal/routing"
    "github.com/goal-web/goal/view"
)

func main() {
    application := app.Singleton()

    routing.Route("GET", "/welcome", func() view.View {
        return view.Make("welcome", map[string]interface{}{
            "name": "Goal",
        })
    })

    application.Run()
}

6. 事件系统

Goal 提供了事件系统,类似于 Laravel 的事件广播。

package main

import (
    "github.com/goal-web/goal/app"
    "github.com/goal-web/goal/events"
)

type UserRegistered struct {
    UserID string
}

func main() {
    application := app.Singleton()

    // 监听事件
    events.Listen(UserRegistered{}, func(event UserRegistered) {
        println("User registered: ", event.UserID)
    })

    // 触发事件
    events.Dispatch(UserRegistered{UserID: "123"})

    application.Run()
}

总结

Goal 框架在 Go 中实现了许多 Laravel 的特性,包括路由、中间件、依赖注入、数据库 ORM、模板引擎和事件系统。如果你熟悉 Laravel,使用 Goal 可以快速上手 Go Web 开发。

回到顶部