社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
最近需要把Go语言写得代码,封装成sdk,提供给app端使用,为此我记录一下我的操作记录。
说实话我也不会啊,就只能花点时间学吧,遇到的最大的一个问题就是翻墙,国内是无法使用,能翻墙的不多说了。
Go语言的优劣,这里就不介绍了,下面直接讲Go 的安装:
Go 的官方网站:http://golang.org/(需要翻墙软件)
国内下载地址:http://www.golangtc.com/download
下载对应平台的安装包。注意区分32位还是64位操作系统。
安装包下载完成之后,安装过程很简单,傻瓜式下一步到底就好了。
安装go 的时候,安装程序会自动把相关目录写到系统环境。但是如果是zip 的安装,需要自己手动添加。
主要配置以下几个:
GOROOT:Go 安装后的根目录,安装过程中会由安装程序自动写入系统环境变量中。
GOBIN:Go 的二进制文件存放目录(%GOROOT%bin)
PATH:需要将 %GOBIN% 加在 PATH 变量的最后,方便在命令行下运行。
当环境变量都配置完成之后,Go 就已经安装完毕了。打开命令行,运行 go 命令,就可以看到如下的提示了。
export GOROOT=/usr/local/Cellar/go/1.11.2/libexec
export PATH=$PATH:$GOPATH/bin
GOPATH : Go 的工作空间,就是我们的开发和依赖包的目录(例如:我的是 D:Go_Pathgo) ,此目录需要手动配置到系统环境变量
GOPATH 工作空间是一个目录层次结构,其根目录包含三个子目录:
export GOPATH=/Users/apple/go
export PATH=$PATH:$GOROOT/bin
注意:
1. IDE 的下载安装这里就不说,大家直接去这个地址下载就行。
Goland:https://www.jetbrains.com/go/download/#section=windows
LiteIDE: https://studygolang.com/dl 这个是最新的1.10.3,免费的IDE
2. 我用的是Atom 编辑器。配置有点麻烦,不建议大家使用。
3. 后面直接讲Go语言的如何使用。
4. 能翻的最好翻墙,因为很多package 在golang官网,不翻墙下载不下来。
相信很多朋友说无法翻墙的吧,我现在交给大家一种不用翻墙也能使用的一种方案。首先确保您能访问github就OK。
我封装成一个shell文件,现在分享给大家。
只需要您配置好GOPATH和GO环境就OK。
#!/bin/bash
if ! [ -x "$(command -v go)" ]; then
echo 'error: go is not installed.'
exit 1
fi
if ! [ -x "$(command -v git)" ]; then
echo 'error: git is not installed.'
exit 1
fi
go version
if [ ! -d "$GOPATH" ]; then
echo "please set GOPATH env"
exit 1
fi
echo "GOPATH: ${GOPATH}"
home=$GOPATH/src/golang.org/x
if [ ! -d "${home}" ]; then
mkdir -p ${home}
fi
cd ${home}
if [ ! -d "${home}/crypto" ]; then
git clone https://github.com/golang/crypto
else
cd ${home}/crypto
git pull
cd ..
fi
if [ ! -d "${home}/exp" ]; then
git clone https://github.com/golang/exp
else
cd ${home}/exp
git pull
cd ..
fi
if [ ! -d "${home}/image" ]; then
git clone https://github.com/golang/image
else
cd ${home}/image
git pull
cd ..
fi
if [ ! -d "${home}/mobile" ]; then
git clone https://github.com/golang/mobile
else
cd ${home}/mobile
git pull
cd ..
fi
if [ ! -d "${home}/net" ]; then
git clone https://github.com/golang/net
else
cd ${home}/net
git pull
cd ..
fi
if [ ! -d "${home}/oauth2" ]; then
git clone https://github.com/golang/oauth2
else
cd ${home}/oauth2
git pull
cd ..
fi
if [ ! -d "${home}/sys" ]; then
git clone https://github.com/golang/sys
else
cd ${home}/sys
git pull
cd ..
fi
if [ ! -d "${home}/text" ]; then
git clone https://github.com/golang/text
else
cd ${home}/text
git pull
cd ..
fi
if [ ! -d "${home}/time" ]; then
git clone https://github.com/golang/time
else
cd ${home}/time
git pull
cd ..
fi
if [ ! -d "${home}/tools" ]; then
git clone https://github.com/golang/tools
else
cd ${home}/tools
git pull
cd ..
fi
if [ ! -d "${home}/sync" ]; then
git clone https://github.com/golang/sync
else
cd ${home}/sync
git pull
cd ..
fi
go get golang.org/x/mobile/cmd/gomobile
gomobile init
gomobile version
echo "gomobile installed success"
gomobile bind -target android xxxxx
gomobile bind -target ios xxxx
如果您觉得对你有用,请为我点赞吧。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!