栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

CentOS7 使用 VScode 运行 Go 程序报错

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

CentOS7 使用 VScode 运行 Go 程序报错

前提条件

1、使用root账户配置GO环境及安装VS Code

2、GO环境变量配置为(cat /etc/profile)

export GO111MODULE=on
export GOROOT=/usr/local/go 
export GOPATH=/home/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

3、使用非root账户(tracy)登录CentOS7可视化界面,使用VS Code编辑并运行GO程序

问题与解决

之后就一直出现问题,一直解决问题,操作步骤如下:

1、使用tracy账户登录CentOS7可视化界面,打开VS Code编写GO程序,如下

package main

import (
	"fmt"
	"github.com/ethereum/go-ethereum/accounts/abi/bind"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/ethclient"
	"log"
)

func main()  {
	//连接geth节点
	cli, err := ethclient.Dial("http://192.168.237.142:8545")
	if err != nil {
		log.Panic("fail to Dial",err)
	}
	defer cli.Close()
	//创建合约实例
	instance, err := NewHello(common.HexToAddress("0xe583E729951686f4F7b9359B022AB04719009AAc"), cli)
	if err != nil {
		log.Panic("error: ", err)
	}
	//函数调用
	opts := bind.CallOpts{
		From: common.HexToAddress("0x778e68Fd9371dE638bD816DAeC7e7B0b2ecaC813"),
	}

	msg, err := instance.GetMsg(&opts)
	if err != nil {
		log.Panic("getMsg error: ", err)
	}

	fmt.Println("?GetMsg:",msg)
}

 2、配置代理(VS Code -> TERMINAL)

[tracy@localhost demo]$ go env -w GOPROXY=https://goproxy.cn 

3、下载依赖,执行go mod tidy报错(第1个)

[tracy@localhost demo]$ go mod init demo
go: creating new go.mod: module demo
[tracy@localhost demo]$ go mod tidy
go: could not create module cache: mkdir /home/gopath/pkg: permission denied

 解决:修改环境变量GOPATH在tracy目录下(vi /etc/profile)

export GO111MODULE=on
export GOROOT=/usr/local/go 
export GOPATH=/home/tracy/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
[root@localhost home]# source /etc/profile

重启VS Code,重新加载环境变量

4、编译报错(第2个)

[tracy@localhost GoContract]$ go build demo
package demo is not in GOROOT (/usr/local/go/src/demo)

 解决:设置GO111MODULE=auto,出现新的错误(第3个)

[tracy@localhost GoContract]$ go env -w GO111MODULE=auto
warning: go env -w GO111MODULE=... does not override conflicting OS environment variable

继续解决:

原因是在设置的时候,系统的环境变量GO111MODULE已经有值,而go env是不支持覆盖写入的。可以直接修改系统环境变量

编辑profile文件(vi /etc/profile),修改export GO111MODULE=auto

export GO111MODULE=auto
export GOROOT=/usr/local/go 
export GOPATH=/home/tracy/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
[root@localhost home]# source /etc/profile

重启VS Code,重新加载环境变量 

再次编译报错 (第4个)

[tracy@localhost demo]$ go build demo
# github.com/ethereum/go-ethereum/crypto/secp256k1
cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in $PATH
# github.com/ethereum/go-ethereum/rpc
cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in $PATH

 解决:安装gcc

[root@localhost home]# yum install -y gcc

再次编译,终于通过了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/903148.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号