golang第六天

学习web框架gin

  • demo代码
package main

import (
    "fmt"
    "github.com/gin-gonic/gin"
    "net/http"
    "log"
    "os"
    "io"
)

/**
 * gin demo代码
 */
func main() {

    /**
     * 创建默认中间件
     */
    router := gin.Default()

    // Disable Console Color
     gin.DisableConsoleColor()

    /**
     *简单测试
     */
    router.GET("/someGet", getting)
    router.POST("/somePost", posting)
    router.PUT("/somePut", putting)
    router.DELETE("/someDelete", deleting)
    router.PATCH("/somePatch", patching)
    router.HEAD("/someHead", head)
    router.OPTIONS("/someOptions", options)



    //How to write log file
    //gin.DisableConsoleColor()

    f, _ := os.Open("tmp/ginlog.log")

    //两个地方存放日志记录
    gin.DefaultWriter = io.MultiWriter(f, os.Stdout)

    /**
     *json返回
     */
    router.GET("/ping", func(context *gin.Context) {
        context.JSON(200, gin.H{
            "message": "pong",
        })
    })

    router.GET("/user/:name", func(context *gin.Context) {
        name := context.Param("name")
        context.String(http.StatusOK, "Hello, %s!", name)
    })


    router.GET("/user/:name/*action", func(context *gin.Context) {
        name := context.Param("name")
        action := context.Param("action")
        message := name + " is " + action
        context.String(http.StatusOK, message)
    })

    /**
     * Querystring parameters
     */
    router.GET("/welcome", func(context *gin.Context) {

        firstName := context.DefaultQuery("firstName", "Guest")
        lastName := context.Query("lastName")
        context.String(http.StatusOK, "Hello, %s-%s", firstName, lastName)
    })

    /**
     * Multipart/Urlencoded Form
     */
    router.POST("/postForm", func(context *gin.Context) {
        message := context.PostForm("message")
        nick := context.DefaultPostForm("nick", "anonymous")

        fmt.Println(message, nick)
        context.JSON(http.StatusOK, gin.H{
            "status":  "posted",
            "message": message,
            "nick":    nick,
        })

    })

    /**
     *处理post表单参数
     */
    router.POST("/post", func(context *gin.Context) {
        id := context.Query("id")
        page := context.DefaultQuery("page", "0")
        name := context.PostForm("name")
        message := context.PostForm("message")
        context.String(http.StatusOK, "id=%s, page=%s, name=%s, message=%s\n", id, page, name, message)
    })

    /**
     *单文件处理
     */
    router.POST("/upload", func(context *gin.Context) {
        file, err := context.FormFile("file")
        if err == nil {
            log.Println(file.Filename)
            err = context.SaveUploadedFile(file, "tmp/" + file.Filename)
            if err == nil {
                context.String(http.StatusOK, "%s success uploaded!", file.Filename)
            }
        } else {
            fmt.Println("err:", err)
        }
    })


    /**
     *多文件处理
     */
    router.POST("/uploadfiles", func(context *gin.Context) {

        form, err := context.MultipartForm()

        if err == nil {
            files := form.File["files"]
            for _, file := range files {
                log.Println(file.Filename)
                err = context.SaveUploadedFile(file, "tmp/" + file.Filename)
                if err == nil {
                    context.String(http.StatusOK, "%s success uploaded!\n", file.Filename)
                }
            }
        } else {
            fmt.Println("err:", err)
        }
    })

    /**
     *router.Run(":3000")
     *默认8080端口
     */
    router.Run()
}

func getting(context *gin.Context) {
    fmt.Println("test getting")
}

func posting(context *gin.Context) {
    fmt.Println("test posting")
}

func putting(context *gin.Context) {
    fmt.Println("test putting")
}

func deleting(context *gin.Context) {
    fmt.Println("test deleting")
}

func patching(context *gin.Context) {
    fmt.Println("test patching")
}

func head(context *gin.Context) {
    fmt.Println("test head")
}

func options(context *gin.Context) {
    fmt.Println("test options")
}

  • 接口测试工具:postman

打卡时间: 23:25

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • 今天语文默写字母,还是拼音卷我写错了“j”,”j”的钩子是往里写了的,我居然把钩子向里外写啦!老师给我画个圈儿,...
    45cbff51831c阅读 281评论 0 1
  • “老师,拜托你了!” 电话那头无奈而又急促的声音传来 “一定要走吗?没有别的办法?现在孩子正需要父母的陪伴……” ...
    幸福_格式化阅读 354评论 0 0
  • 一段文,即使触动,也翻不起涟漪,我知道,这是真的走过了。 一首歌,再喜欢,也不会对号入座,我知道,这是真的沉淀了。...
    余说新语阅读 249评论 0 0