美文网首页
7.Beego框架之 config、httplib、contex

7.Beego框架之 config、httplib、contex

作者: 编程_书恨少 | 来源:发表于2019-04-30 10:37 被阅读0次

一、config配置文件解析

go get github.com/astaxie/beego/config

1.首先初始化一个解析器对象

image.png

2.解析器对象支持的函数有如下:

image.png

3.解析器对象支持的函数有如下:

ini 配置文件支持 section 操作,key通过 section::key 的方式获取

        例如下面这样的配置文件

        [demo]
        key1 = "asta"
        key2 = "xie"

        那么可以通过 iniconf.String("demo::key2") 获取值

二、httplib

httplib 库主要用来模拟客户端发送 HTTP 请求,类似于 Curl 工具,支持 JQuery 类似的链式操作。使用起来相当的方便;通过如下方式进行安装:

image.png image.png

这样就可以把百度的内容爬下来

package controllers

import (
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/httplib"
)

type TestHttpLibController struct {
    beego.Controller
}

func (c *TestHttpLibController) Get() {
    req := httplib.Get("http://www.baidu.com")
    str,err:=req.String()

    if err!=nil{
        panic(err)
    }

    c.Ctx.WriteString(str)
}   

3.context

image.png image.png

通过Input对象获取客户端参数

示例:


image.png

相关文章

网友评论

      本文标题:7.Beego框架之 config、httplib、contex

      本文链接:https://www.haomeiwen.com/subject/afpunqtx.html