安裝iris框架


1、導語

目前Go語言已經為大多數人所熟知,越來越多的開發人員選擇使用Go語言來進行開發,但是如何使用

Go來進行web開發,在其他編程語言中都有對應的開發框架,當然在Go中也有,就是即將要介紹的——iris,它號稱為速度最快的Go后端開發框架。在iris的網站文檔上,列出該框架具備的一些特點和框架特性,列舉如下:

1)聚焦高性能
2)健壯的靜態路由支持和通配符子域名支持
3)視圖系統支持超過5以上模板
4)支持定制事件的高可擴展性Websocket API
5)帶有GC, 內存 & redis 提供支持的會話
6)方便的中間件和插件
7)完整 REST API
8)能定制 HTTP 錯誤
9)源碼改變后自動加載
等等還有很多特性,大家可以參考Iris官方文檔。

2、iris框架學習推薦網站

官網:https://iris-go.com/

框架源碼地址:https://github.com/kataras/iris

中文學習文檔:https://learnku.com/docs/iris-go/10

3、iris框架安裝

官網安裝方式:

> go get -u github.com/kataras/iris

注意:iris要求你當前的go語言環境最低是1.8,但是官網推薦1.9以上

但是,由於GitHub上的資源,咱們在國內直接拉取會出現安裝失敗的問題,就例如:

# cd .; git clone https://github.com/kataras/iris D:\go_workspace\src\github.com\kataras\iris
Cloning into 'D:\go_workspace\src\github.com\kataras\iris'...
fatal: early EOF
fatal: The remote end hung up unexpectedly
fatal: index-pack failed
error: RPC failed; curl 18 transfer closed with outstanding read data remaining
package github.com/kataras/iris: exit status 128

出現這種情況,不要慌,這是因為在國內訪問GitHub會出問題,所以在我們安裝Go語言中的一些包的安裝就需要通過代理來實現

3.1、Go模塊的全球代理

Linux\macOS

將以下指令添加到當前操作系統的環境變量當中.bashrc或者.bash_profile文件

# Enable the go modules feature
export GO111MODULE=on

# Set the GOPROXY environment variable
export GOPROXY=https://goproxy.io

windows

在windows中執行以下指令

# Enable the go modules feature
$env:GO111MODULE="on"

# Set the GOPROXY environment variable
$env:GOPROXY="https://goproxy.io"

現在,在構建和運行應用程序時,go將通過goproxy.io獲取依賴項。在goproxy儲存庫中查看更多信息。

如果你的Go版本> = 1.13,則GOPRIVATE環境變量將控制go命令認為哪個模塊是私有的(不可公開使用),因此不應使用代理或校驗和數據庫。例如:

go env -w GOPROXY=https://goproxy.io,direct

# Set environment variable allow bypassing the proxy for selected modules
go env -w GOPRIVATE=*.corp.example.com

將代理設置完之后,就可以來執行,官方指定的安裝方式:

> go get -u github.com/kataras/iris

3.2、測試

package main

import (
	"github.com/kataras/iris"
)

func main() {
	app := iris.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 監聽端口
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM