【GO】Iris框架項目初始化並解決GoLand的代碼提示問題

爲什麼使用Iris

因爲它是少數Go框架中支持MVC的框架,並且是最快的go框架

具體操作

創建項目

創建項目 IDE採用goland,直接選擇第一個來創建項目

在這裏插入圖片描述

初始化mod

go mod 可以讓你擺脫GOPATH對項目的約束,同時也是解決GoLand問題的核心

export GO111MODULE=on
lixiaoyu@localhost projectApi % go mod init projectApi
go: creating new go.mod: module projectApi

引入Iris

 go get -u github.com/kataras/iris

go會從github中下載文件到本地,執行完成之後,我們得到的go.mod內容如下

module projectApi

go 1.14

require (
   github.com/BurntSushi/toml v0.3.1 // indirect
   github.com/Joker/jade v1.0.0 // indirect
   github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398 // indirect
   github.com/aymerick/raymond v2.0.2+incompatible // indirect
   github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 // indirect
   github.com/fatih/structs v1.1.0 // indirect
   github.com/flosch/pongo2 v0.0.0-20200509134334-76fc00043fe1 // indirect
   github.com/gorilla/schema v1.1.0 // indirect
   github.com/iris-contrib/blackfriday v2.0.0+incompatible // indirect
   github.com/iris-contrib/formBinder v5.0.0+incompatible // indirect
   github.com/iris-contrib/go.uuid v2.0.0+incompatible // indirect
   github.com/json-iterator/go v1.1.9 // indirect
   github.com/juju/errors v0.0.0-20200330140219-3fe23663418f // indirect
   github.com/kataras/golog v0.0.13 // indirect
   github.com/kataras/iris v11.1.1+incompatible // indirect
   github.com/klauspost/compress v1.10.5 // indirect
   github.com/microcosm-cc/bluemonday v1.0.2 // indirect
   github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
   github.com/modern-go/reflect2 v1.0.1 // indirect
   github.com/ryanuber/columnize v2.1.0+incompatible // indirect
   github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
   golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 // indirect
   golang.org/x/net v0.0.0-20200506145744-7e3656a0809f // indirect
   golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25 // indirect
   golang.org/x/text v0.3.2 // indirect
   gopkg.in/yaml.v2 v2.2.8 // indirect
)

創建項目入口main.go

main.go的內容如下,這裏僅僅是個示例,目的是展示入口引入的庫。

package main

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

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


func newApp() *iris.Application {
	app := iris.New()
	app.Use(recover.New())
	app.Use(logger.New())

	mvc.New(app).Handle(new(ExampleController))
	return app
}

func main() {
	app := newApp()

	app.Run(iris.Addr(":8888"))
}

type ExampleController struct{}

func (c *ExampleController) Get() mvc.Result {
	return mvc.Response{
		ContentType: "text/html",
		Text:        "<h1>Welcome</h1>",
	}
}

func (c *ExampleController) GetPing() string {
	return "pong"
}

func (c *ExampleController) GetHello() interface{} {
	return map[string]string{"message": "Hello Iris!"}
}

func (c *ExampleController) BeforeActivation(b mvc.BeforeActivation) {
	anyMiddlewareHere := func(ctx iris.Context) {
		ctx.Application().Logger().Warnf("Inside /custom_path")
		ctx.Next()
	}
	b.Handle("GET", "/custom_path", "CustomHandlerWithoutFollowingTheNamingGuide", anyMiddlewareHere)

}

func (c *ExampleController) CustomHandlerWithoutFollowingTheNamingGuide() string {
	return "hello from the custom handler without following the naming guide"
}

這時候我們發現相關的引用是錯誤的,我們也不能使用代碼提示。

在這裏插入圖片描述

但是項目是可以運行的。

在這裏插入圖片描述

外部庫下載到本地

雖然現在能用,但是作爲開發者的體驗極差,所以我們需要把庫引入到項目的目錄下。

lixiaoyu@localhost projectApi % go mod vendor 

執行之後,我們會發現項目的根目錄下增加了一個vendor的文件夾,文件夾的名字和PHP的composer生成的文件夾相同,當然作用也是一樣的。

在這裏插入圖片描述

這時main.go的引用和代碼提示也都正常了。

在這裏插入圖片描述

回顧

經過這一系列的操作,我們得到了一個項目基礎。

在這裏插入圖片描述

包含了

  • 入口文件
  • go.mod
  • vendor

然後在這基礎上可以進行功能的開發了。

參考資料

  • https://iris-go.com/
  • https://golang.google.cn/cmd/go/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章