亲宝软件园·资讯

展开

golang框架iris mac下安装golang框架iris的方法

李高峰的博客 人气:0

安装前准备:

设置GOPROXY代理

参考:https://goproxy.cn/

我使用的是下面的两条命令:

$ go env -w GO111MODULE=on
$ go env -w GOPROXY=https://goproxy.cn,direct

我的go env信息:

% go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/mac/Library/Caches/go-build"
GOENV="/Users/mac/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/mac/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/mac/go"
GOPRIVATE=""
GOPROXY="http://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/mac/www/iris/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/d5/13f52jg52n500y4w_8bxcx680000gn/T/go-build475857461=/tmp/go-build -gno-record-gcc-switches -fno-common"
mac@MacdeMacBook-Pro iris % go env -w GO111MODULE=on
mac@MacdeMacBook-Pro iris % go env -w GOPROXY=https://goproxy.cn,direct
mac@MacdeMacBook-Pro iris % go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/mac/Library/Caches/go-build"
GOENV="/Users/mac/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/mac/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/mac/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/mac/www/iris/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/d5/13f52jg52n500y4w_8bxcx680000gn/T/go-build212739672=/tmp/go-build -gno-record-gcc-switches -fno-common"

安装过程参考:https://www.iris-go.com/docs/

创建一个新项目:

% cd ~/www
% mkdir iris 
% cd iris 
% go mod init iris
go: creating new go.mod: module iris
% ls
go.mod
%vim main.go
package main
 
import (
 "github.com/kataras/iris/v12"
)
 
func main() {
 app := iris.New()
 app.Get("/", func(ctx iris.Context) {
  ctx.HTML("<h1>Hello World!</h1>")
  app.Logger().Info("我在学习Iris噢")
 })
 app.Run(iris.Addr(":8009"))
}

实际安装过程:

go get -u github.com/kataras/iris/v12@master
 
go: downloading github.com/kataras/iris/v12 v12.2.0-alpha.0.20201126085352-5b6802d00eaf
go: downloading github.com/kataras/iris v0.0.0-20191006184023-c8e73f4f4df2
go: github.com/kataras/iris/v12 master => v12.2.0-alpha.0.20201126085352-5b6802d00eaf
go: downloading github.com/tdewolff/minify/v2 v2.9.10
go: downloading github.com/BurntSushi/toml v0.3.1
go: downloading github.com/kataras/tunnel v0.0.2
go: downloading github.com/mailru/easyjson v0.7.6
go: downloading gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
go: downloading github.com/json-iterator/go v1.1.10
go: downloading github.com/schollz/closestmatch v2.1.0+incompatible
go: downloading github.com/fatih/structs v1.1.0
go: downloading github.com/CloudyKit/jet/v5 v5.1.1
go: downloading github.com/flosch/pongo2/v4 v4.0.0
go: downloading github.com/kataras/golog v0.1.5
go: downloading gopkg.in/ini.v1 v1.62.0
go: downloading golang.org/x/text v0.3.4
go: downloading github.com/kataras/blocks v0.0.4
go: downloading github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
go: downloading github.com/tdewolff/parse/v2 v2.5.5
go: downloading github.com/kataras/pio v0.0.10
go: downloading github.com/iris-contrib/jade v1.1.4
go: downloading google.golang.org/protobuf v1.25.0
go: downloading github.com/yosssi/ace v0.0.5
go: downloading github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53
go: downloading golang.org/x/sys v0.0.0-20201119102817-f84b799fce68
go: downloading github.com/google/uuid v1.1.2
go: downloading github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible
go: downloading golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
go: downloading github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742
go: downloading golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
go: downloading github.com/russross/blackfriday v1.5.2
go: downloading github.com/klauspost/compress v1.11.3
go: downloading github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385
go: downloading github.com/andybalholm/brotli v1.0.1
go: downloading github.com/vmihailenco/msgpack/v5 v5.0.0
go: downloading github.com/russross/blackfriday/v2 v2.1.0
go: downloading github.com/iris-contrib/schema v0.0.6
go: downloading github.com/vmihailenco/tagparser v0.1.2
go: downloading github.com/kataras/sitemap v0.0.5
go: downloading github.com/valyala/bytebufferpool v1.0.0
go: downloading golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392
go: downloading github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398
go: downloading github.com/microcosm-cc/bluemonday v1.0.4
go: downloading github.com/chris-ramon/douceur v0.2.0
go: downloading github.com/aymerick/douceur v0.2.0
go: downloading github.com/gorilla/css v1.0.0

以上安装在了~/go/pkg/mod目录下了:

创建一个main.go文件:

%vim main.go
package main
 
import (
 "github.com/kataras/iris/v12"
)
 
func main() {
 app := iris.New()
 app.Get("/", func(ctx iris.Context) {
  ctx.HTML("<h1>Hello World!</h1>")
  app.Logger().Info("我在学习Iris噢")
 })
 app.Run(iris.Addr(":8009"))
}
 
以上保存后运行:
% go run main.go 
Now listening on: http://localhost:8009
Application started. Press CMD+C to shut down.

用goland打开刚才的iris项目发现不能识别iris,引入的iris部分提示红色,解决办法为:Preferences->Go Modules

保存即可

外部库下载到项目根目录下:

 % go mod vendor 

执行之后,项目的根目录下增加了一个vendor的文件夹,文件夹的名字和PHP的composer生成的文件夹相同,作用也是一样的。

然后在这基础上可以进行功能的开发了。

加载全部内容

相关教程
猜你喜欢
用户评论