首页 > 系统相关 >Linux环境安装go部署运行gin框架

Linux环境安装go部署运行gin框架

时间:2023-01-27 00:23:20浏览次数:41  
标签:Linux 09 debug GIN go gin root

1、Centos7.9解压安装go1.18

[root@node01 09:21:08/usr/local]#cd /usr/local/ && tar xf go1.18.1.linux-amd64.tar.gz 

2、添加环境变量

vim /etc/profile
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH

3、检查是否成功

[root@master 09:12:24 /data/go/gin_demo]# go version
go version go1.18.1 linux/amd64

4、初始化mod

# 创建web目录
mkdir /data/go/gin_demo

# 初始化mod 用来管理go的依赖包
[root@node01 09:50:39/data/go/gin_demo]# go  mod init gin
go: creating new go.mod: module gin
go: to add module requirements and sums:
        go mod tidy

# 走国内代理下载
[root@node01 09:51:49/data/go/gin_demo]# go env -w GOPROXY=https://goproxy.cn,direct  

# 下载gin包,这是一个web框架
[root@node01 09:51:10/data/go/gin_demo]# go get github.com/gin-gonic/gin

5、编写main.go文件

[root@node01 09:38:40/data/go/gin_demo]# vim main.go
package main

import "github.com/gin-gonic/gin"

func sayHello(c *gin.Context) {
	c.JSON(200, gin.H{
		"message": "Hello golang!",
	})
}

func main() {
	r := gin.Default() // 返回默认的路由引擎

	r.GET("/hello", sayHello)

	// 启动服务
	r.Run()
}

6、运行是否成功

[root@node01 09:52:55/data/go/gin_demo]# go run main.go                             
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /hello                    --> main.sayHello (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080

7、访问测试

 

 

 

  

 

  

  

标签:Linux,09,debug,GIN,go,gin,root
From: https://www.cnblogs.com/lucktomato/p/17068432.html

相关文章

  • Android View VISIBLE,INVISIBLE和GONE的区别
    AndroidViewVISIBLE,INVISIBLE和GONE的区别首先整理一下View的三种显示的形式,VISIBLE,INVISIBLE和GONE。/***Thisviewisvisible.*Usewith{@link......
  • vs code设置上传下载文件到Linux
    1、打开vscode工具,点击扩展,选择SFTP安装  2、CTRL+SHIFT+P打开配置SFTP    3、选择上传下载,会提示输入密码,输入密码后开始传输文件 ......
  • The combination of Jianshu, Linux, Firefox told me that Jianshu is a SB Wanyier.
    403ForbiddenYoudon'thavepermissiontoaccesstheURLonthisserver.Sorryfortheinconvenience.Pleasereportthismessageandincludethefollowinginf......
  • 初入职场的我认识到linux的重要性
    先自我介绍下,我是一名普通的软件工程专业的本科毕业生,今年刚毕业,软件工程的课程超级多,有程序设计基础、面向对象程序设计、软件工程导论、离散结构、数据结构与算法、计算......
  • docker中使用nginx
    1.拉取镜像拉取测试浏览器测试参考#安装https://www.cnblogs.com/zhaokejin/p/15605261.html2.修改配置我们再开启一个终端,进入到nginx容器中但我们发现vim、......
  • Nginx读取后端服务响应数据流程
    gdbattach[worker进程号]在指定文件的898行打上断点bsrc/event/modules/ngx_epoll_module.c:898客户端发送请求按3次c以后,按n(函数单行执行)和s(函数逐行执行)单步调......
  • AtCoder Beginner Contest 159
    A-TheNumberofEvenPairs相加为偶数只有偶加偶和奇加奇两种情况,其实就是在\(n\)个数中取两个(\(C\binom{2}{n}\)),在\(m\)个数中取两个(\(C\binom{2}{m}\))。B-String......
  • Linux笔记03: Linux常用命令_3.5权限管理命令
     3.5权限管理命令3.5.1权限介绍1.为什么需要权限绝大多数用户使用的是个人计算机,而使用个人计算机的用户一般都是被信任的用户(如家人、朋友等)。在这种情况下,大家都可......
  • P5858 「SWTR-03」Golden Sword DP+单调队列模板
    P5858「SWTR-03」GoldenSword-洛谷|计算机科学教育新生态(luogu.com.cn) 理解题意后,我们知道贪心和暴力枚举显然是不行的,联想到DP我们设置dp[i][j]表示,第i种......
  • 搭建开发环境 -- linux系统
    前言 默认使用vagrant搭建环境,此环境可以不占用本地端口,即插即用,使用的工具有:docker  对应的控制台工具  portainermysqlredisnacos ===================......