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
再次编译,终于通过了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)