Iris 框架安装步骤

前言

今天准备开始使用 Go 开发,用 iris 框架,记录安装过程。

环境

win10 专业版 1903

Go 1.13.3

准备工作

  • 配置host
# 感觉有点玄学,不知道有没有起到实际作用
192.30.253.113 github.com
151.101.185.194 github.global.ssl.fastly.net

安装步骤

  • $GOPATH 目录下执行 go get -v github.com/kataras/iris 下载时间可能有点久
  • 创建目录 $GOPATH\src\golang.org\x ,在该目录下执行
git clone git@github.com:golang/net.git
git clone git@github.com:golang/crypto.git
git clone git@github.com:golang/text.git

我安装完成之后是这样的,有几个是之前安装

Iris 框架安装步骤

测试

$GOPATH 目录下新建 iris文件夹 ,新建 server.go,内容如下

package main

import (
    "github.com/kataras/iris"
    "github.com/kataras/iris/middleware/logger"
    "github.com/kataras/iris/middleware/recover"
)

func main() {
    app := iris.New()
    app.Use(recover.New())
    app.Use(logger.New())
    //输出html
    // 请求方式: GET
    // 访问地址: http://localhost:8080/welcome
    app.Handle("GET", "/welcome", func(ctx iris.Context) {
        ctx.HTML("<h1>Welcome</h1>")
    })
    //输出字符串
    // 类似于 app.Handle("GET", "/ping", [...])
    // 请求方式: GET
    // 请求地址: http://localhost:8080/ping
    app.Get("/ping", func(ctx iris.Context) {
        ctx.WriteString("pong")
    })
    //输出json
    // 请求方式: GET
    // 请求地址: http://localhost:8080/hello
    app.Get("/hello", func(ctx iris.Context) {
        ctx.JSON(iris.Map{"message": "Hello Iris!"})
    })
    app.Run(iris.Addr(":8080")) //8080 监听端口
}

执行 go run server.go

$ go run server.go
Now listening on: http://0.0.0.0:8080
Application started. Press CTRL+C to shut down.

在浏览器中访问

http://localhost:8080/welcome
http://localhost:8080/hello
http://localhost:8080/ping

如下图

Iris 框架安装步骤

Iris 框架安装步骤

Iris 框架安装步骤

终端输出

[INFO] 2019/10/28 19:17 200   0s ::1 GET /welcome
[INFO] 2019/10/28 19:17 200   0s ::1 GET /hello
[INFO] 2019/10/28 19:17 200   0s ::1 GET /ping

如果有什么错误的地方,希望大家能指出,一起学习进步。

本作品采用《CC 协议》,转载必须注明作者和本文链接
感谢阅读,有收获的话不妨点个赞:smiling_imp:
讨论数量: 2
pardon110

下载时间久,可以考虑配置GOPROXY,七牛云不错

4年前 评论

@pardon110 你好,配置了 GOPROXY 之后速度快了不少,输出如下

$ go get -v github.com/kataras/iris
github.com/kataras/iris (download)
github.com/BurntSushi/toml (download)
github.com/kataras/golog (download)
github.com/kataras/pio (download)
github.com/Shopify/goreferrer (download)
package golang.org/x/net/publicsuffix: unrecognized import path "golang.org/x/net/publicsuffix" (https fetch: Get https://golang.org/x/net/publicsuffix?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
github.com/fatih/structs (download)
github.com/iris-contrib/blackfriday (download)
github.com/shurcooL/sanitized_anchor_name (download)
github.com/iris-contrib/schema (download)
github.com/json-iterator/go (download)
github.com/modern-go/concurrent (download)
github.com/modern-go/reflect2 (download)
github.com/klauspost/compress (download)
github.com/microcosm-cc/bluemonday (download)
github.com/chris-ramon/douceur (download)
github.com/gorilla/css (download)
package golang.org/x/net/html: unrecognized import path "golang.org/x/net/html" (https fetch: Get https://golang.org/x/net/html?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
get "gopkg.in/yaml.v2": found meta tag get.metaImport{Prefix:"gopkg.in/yaml.v2", VCS:"git", RepoRoot:"https://gopkg.in/yaml.v2"} at //gopkg.in/yaml.v2?go-get=1
gopkg.in/yaml.v2 (download)
package golang.org/x/crypto/acme/autocert: unrecognized import path "golang.org/x/crypto/acme/autocert" (https fetch: Get https://golang.org/x/crypto/acme/autocert?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
github.com/ryanuber/columnize (download)
github.com/CloudyKit/jet (download)
github.com/Joker/jade (download)
github.com/aymerick/raymond (download)
github.com/eknkc/amber (download)
github.com/flosch/pongo2 (download)
github.com/juju/errors (download)

net/publicsuffix
/net/html
/crypto/acme/autocert 这三个包还是要另外处理吗?

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!