Golang环境搭建以及gomobile开发Android、ios的第三方库 - Go语言中文社区

Golang环境搭建以及gomobile开发Android、ios的第三方库


最近需要把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_Pathgo) ,此目录需要手动配置到系统环境变量
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 ! [ -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"

6、使用

  • 编译Android引用库
gomobile bind -target android xxxxx
  • 编译ios引用库,需要安装xcode
gomobile bind -target ios xxxx

7、结束语

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

版权声明:本文来源简书,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.jianshu.com/p/d6d6af4cac4d
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-01-12 11:45:20
  • 阅读 ( 2188 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢