Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build iris Environment for go

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to build go iris environment, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

Local environment

Windows 10

Go Version 1.7.3

IDEA 2020.2

Go configuration directory

Local directory (GOROOT): d:\ Go

Project directory (GOPATH): d:\ GoProjects

GOSDK:D:\ Go\ bin

Directory description: since mod has been supported since Go version 1.2x, our project no longer has to be in the SCR directory of GoPATH. Instead, it is declared to be our Go project through go mod, and then the corresponding execution file and the required dependencies are all under the bin and pkg packages of GOPATH.

Create an example

Create under SCR

Just create a package under SCR.

That is, directly create a folder as the project directory

In other directories

The most intuitive thing you can see when IDEA is created is that there is an extra Module option in other directories, which is actually creating a stand-alone project.

The creation process is as follows.

Then you will see a go.mod file in your project directory

Module Test0

Go 1.17

The default initial is as follows.

In addition, if you create it manually, go to the following steps (but this is obviously a bit painful in engineering development)

Go mod init yourprojectname / / generate mod file

After that

Go build / / generate the executable file or in the current directory

Go install / / generate in bin under your project GoPATH

Why do you say this? the reason is very simple. I have to say that after stepping on some holes, there is our dependence.

We usually download third-party packages through go get.

First of all, we know that Go generally looks for third-party dependencies under GOROOT/scr and GOPATH. Now the third-party packages we download are generally available in

GOPATH\ pkg\ mod

For example, on my side:

Install IrIs

Due to special reasons, we can't download and understand everything directly from Github.

So we need to configure the download image.

Temporary configuration

Like python, there are temporary and permanent.

It's temporary. That's all you need.

Windows

Set GO111MODULE= "on"

Set GOPROXY= https://goproxy.cn

Then execute the download command

Go get gopkg.in/russross/blackfriday.v2

Linux

Similar

Export GO111MODULE= "on"

Export GOPROXY= https://goproxy.cn

Permanent configuration

That is to write it directly into the environment variable.

We all know it. It's convenient to demonstrate by windows here.

Then download

Start the project

Follow the previous procedure to configure the project

Here we need to use IDEA to help us.

Since we are not under scr, we can rely on our related environment through go.mod, but idea can help us avoid the hassle of mod files.

Start writing main.go

We import the package directly.

At this point, IDEA will automatically find dependencies.

Import it for us. If you are in the scr directory (GOPATH), then don't worry about it. I'm just not used to doing this.

Automatically fix and modify the corresponding mod file at this time

And the dependency has been imported

The first Iris case

This is to take the official case directly.

Package mainimport "github.com/kataras/iris/v12" func main () {app: = iris.Default () app.Use (myMiddleware) app.Handle ("GET", "/ ping", func (ctx iris.Context) {ctx.JSON (iris.Map {"message": "pong"})}) / Listens and serves incoming http requests / / on http://localhost:8080. App.Run (iris.Addr (": 8080"))} func myMiddleware (ctx iris.Context) {ctx.Application () .Logger () .Infof ("Runs before% s", ctx.Path ()) ctx.Next ()}

At this time

Go run main.go

Running

The above is all the content of this article "how to build an iris environment for go". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report