美文网首页Android进阶之路Android 开发技术分享
Golang环境搭建以及gomobile国内使用

Golang环境搭建以及gomobile国内使用

作者: zhangchunlin | 来源:发表于2019-09-28 16:30 被阅读0次

最近需要把Go语言写得代码,封装成sdk,提供给app端使用,为此我记录一下我的操作记录。
说实话我也不会啊,就只能花点时间学吧,遇到的最大的一个问题就是翻墙,国内是无法使用,能翻墙的不多说了。

1、安装 Go

Go语言的优劣,这里就不介绍了,下面直接讲Go 的安装:
Go 的官方网站http://golang.org/(需要翻墙软件)
国内下载地址http://www.golangtc.com/download
下载对应平台的安装包。注意区分32位还是64位操作系统。
安装包下载完成之后,安装过程很简单,傻瓜式下一步到底就好了。

2、Go 环境变量

安装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

3、Go 工作空间

GOPATH : Go 的工作空间,就是我们的开发和依赖包的目录(例如:我的是 D:\Go_Path\go) ,此目录需要手动配置到系统环境变量
GOPATH 工作空间是一个目录层次结构,其根目录包含三个子目录:

  • src:包含 Go 源文件,注意:你自己创建依赖的package,也要放到GOPATH 目录下,这样才能够被引用到。
  • pkg:包含包对象,编译好的库文件
  • bin:包含可执行命令
export GOPATH=/Users/apple/go
export PATH=$PATH:$GOROOT/bin

注意:

  1. 需要将GOPATH 路径,手动写入到系统环境变量。
  2. 不要把 GOPATH 设置成 Go 的安装路径
  3. 你自己创建依赖的package,也要放到GOPATH 目录下,这样才能够被引用到。

4、其他

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官网,不翻墙下载不下来。

5、gomobile 安装

相信很多朋友说无法翻墙的吧,我现在交给大家一种不用翻墙也能使用的一种方案。首先确保您能访问github就OK。
我封装成一个shell文件,现在分享给大家。
只需要您配置好GOPATH和GO环境就OK。

#!/bin/bash
if [ ! -d "$GOPATH" ]; then
    echo "please set GOPATH env"
    exit 0
fi
home=$GOPATH/src/golang.org/x
echo "gomobile path is ${home}"
if [ ! -d "${home}" ]; then
    mkdir -p ${home}
fi
cd ${home}
if [ ! -d "${home}/crypto" ]; then
    git clone https://github.com/golang/crypto
fi
if [ ! -d "${home}/exp" ]; then
    git clone https://github.com/golang/exp
fi
if [ ! -d "${home}/image" ]; then
    git clone https://github.com/golang/image
fi
if [ ! -d "${home}/mobile" ]; then
    git clone https://github.com/golang/mobile
fi
if [ ! -d "${home}/net" ]; then
    git clone https://github.com/golang/net
fi
if [ ! -d "${home}/oauth2" ]; then
    git clone https://github.com/golang/oauth2
fi
if [ ! -d "${home}/sys" ]; then
    git clone https://github.com/golang/sys
fi
if [ ! -d "${home}/text" ]; then
    git clone https://github.com/golang/text
fi
if [ ! -d "${home}/time" ]; then
    git clone https://github.com/golang/time
fi
if [ ! -d "${home}/tools" ]; then
    git clone https://github.com/golang/tools 
fi
go get golang.org/x/mobile/cmd/gomobile
gomobile init
gomobile version
echo "gomobile env success"

6、结束语

如果您觉得对你有用,请为我点赞吧。

相关文章

网友评论

    本文标题:Golang环境搭建以及gomobile国内使用

    本文链接:https://www.haomeiwen.com/subject/viizuctx.html