我想在Google Colab上使用Go语言
谷歌协作平台
如何在Google Colab上运行Golang。
-
- 运行第一个代码单元格,使用 Python 运行时。
-
- 刷新页面以使用 Go 内核(gophernotes)。
- 通过运行下面的 Golang 代码单元格测试一切是否正常。
!add-apt-repository ppa:longsleep/golang-backports -y
!apt update
!apt install golang-go
%env GOPATH=/root/go
!go get -u github.com/gopherdata/gophernotes
!cp ~/go/bin/gophernotes /usr/bin/
!mkdir /usr/local/share/jupyter/kernels/gophernotes
!cp ~/go/src/github.com/gopherdata/gophernotes/kernel/* \
/usr/local/share/jupyter/kernels/gophernotes
Rf。
https://www.ptrace-security.com/blog/how-to-run-golang-in-google-colab/
请参考这个链接:https://www.ptrace-security.com/blog/how-to-run-golang-in-google-colab/
Ign:2 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease
Get:3 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3,626 B]
Get:4 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release [697 B]
Hit:5 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release
Get:6 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release.gpg [836 B]
Get:7 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:8 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease [15.9 kB]
Hit:9 http://archive.ubuntu.com/ubuntu bionic InRelease
Get:11 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Ign:12 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages
Get:12 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages [740 kB]
Hit:13 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease
Get:14 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [395 kB]
Get:15 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease [15.9 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:17 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [2,116 kB]
Hit:18 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease
Get:19 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [24.5 kB]
Get:20 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1,410 kB]
Get:21 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic InRelease [15.4 kB]
Get:22 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [2,546 kB]
Get:23 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main Sources [1,756 kB]
Get:24 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [31.4 kB]
Get:25 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [425 kB]
Get:26 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2,181 kB]
Get:27 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main amd64 Packages [899 kB]
Get:28 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 Packages [39.5 kB]
Get:29 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 Packages [6,000 B]
Fetched 12.9 MB in 5s (2,652 kB/s)
读取软件包列表… 完成
Ign:1 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease
Hit:2 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease
Ign:3 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease
Hit:4 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release
Hit:5 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release
Hit:6 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease
Hit:7 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:8 http://archive.ubuntu.com/ubuntu bionic InRelease
Hit:10 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease
Hit:11 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:13 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:14 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease
Hit:15 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease
Hit:16 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic InRelease
读取软件包列表… 完成
构建依赖关系树
读取状态信息… 完成
将同时安装下列软件包:
golang-1.16-go golang-1.16-src golang-src
建议安装:
bzr | brz mercurial subversion
将安装下列新软件包:
golang-1.16-go golang-1.16-src golang-go golang-src
升级了 0 个软件包,新安装了 4 个软件包,要卸载 0 个软件包,有 79 个软件包未升级。
需要下载 65.7 MB 的归档文件。
完成后会使用掉 377 MB 的额外空间。
获取:1 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-1.16-src amd64 1.16.3-2longsleep1+focal [14.3 MB]
获取:2 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-1.16-go amd64 1.16.3-2longsleep1+focal [51.4 MB]
获取:3 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-src amd64 2:1.16-1longsleep1+bionic [3,952 B]
获取:4 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-go amd64 2:1.16-1longsleep1+bionic [23.0 kB]
已下载 65.7 MB,耗时 6s (10.6 MB/s)
正在选中未选择的软件包 golang-1.16-src。
(正在读取数据库 …当前已安装 160983 个文件和目录。)
正在准备解包 …/golang-1.16-src_1.16.3-2longsleep1+focal_amd64.deb …
正在解包 golang-1.16-src (1.16.3-2longsleep1+focal) …
正在选中未选择的软件包 golang-1.16-go。
正在准备解包 …/golang-1.16-go_1.16.3-2longsleep1+focal_amd64.deb …
正在解包 golang-1.16
上面的部分不工作。
这个脚本安装失败。
!cp ~/go/src/github.com/gopherdata/gophernotes/kernel/*
在这个地方出错。
然而,似乎没有大错误。感觉已经达到了一个好的程度。
可能是在指定目录时出现了错误,在GitHub上进行了程序版本更新。需要找出从GitHub复制到Colab本地的核心(有点复杂)所在的位置。
使用以下脚本。
在前一个部分的脚本中,安装会出现错误。
很可能是因为~/go/src/github.com/路径不存在。
请尝试以下脚本进行测试。安装大约需要2分钟。
安装完成后,将重新加载页面。
参考。
https://gist.github.com/korakot/132725732d9bef9cf5f97654e1addd1f#gistcomment-3660446
# run this cell first time using python runtime
## Update package-registry
! add-apt-repository ppa:longsleep/golang-backports -y
! apt update
## Install golang and set env-vars
! apt install golang-go
GOPATH="/root/go"
! export GOPATH
## Install gophernotes
REPO_PACKAGE="github.com/gopherdata/gophernotes"
! go get -u $REPO_PACKAGE
! cp "$GOPATH/bin/gophernotes" /usr/bin/
## Clone gophernotes repo
REPO_REMOTE=f"https://{REPO_PACKAGE}"
REPO_LOCAL=f"{GOPATH}/src/{REPO_PACKAGE}"
! mkdir -p $REPO_LOCAL
! git clone "$REPO_REMOTE" "$REPO_LOCAL" || \
(cd "$REPO_LOCAL" ; git pull)
! cd /content/
## Copy files under kernel directory
GOPHERNOTES_KERNELS="/usr/local/share/jupyter/kernels/gophernotes"
! mkdir -p $GOPHERNOTES_KERNELS
! cp $REPO_LOCAL/kernel/* $GOPHERNOTES_KERNELS
# then refresh, it will now use gophernotes. Skip to golang in later cells
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:3 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3,626 B]
Ign:4 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease
Get:5 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease [15.9 kB]
Hit:6 http://archive.ubuntu.com/ubuntu bionic InRelease
Get:7 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release [697 B]
Hit:8 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release
Get:9 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release.gpg [836 B]
Get:10 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Hit:11 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease
Get:12 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:13 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease [15.9 kB]
Hit:14 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease
Get:15 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [24.5 kB]
Get:16 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [2,116 kB]
Get:17 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic InRelease [15.4 kB]
Get:18 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1,410 kB]
Get:19 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [395 kB]
Ign:21 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages
Get:21 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages [739 kB]
Get:22 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main Sources [1,756 kB]
Get:23 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [2,546 kB]
Get:24 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main amd64 Packages [899 kB]
Get:25 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [425 kB]
Get:26 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [31.4 kB]
Get:27 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2,182 kB]
Get:28 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 Packages [39.5 kB]
Get:29 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 Packages [6,000 B]
取得了12.9 MB的存档,用时4秒(3,319 kB/s)
读取软件包列表… 完成
正在读取状态信息… 完成
有78个软件包可以升级。运行’apt list –upgradable’命令可以查看它们。
读取软件包列表… 完成
正在读取状态信息… 完成
将安装以下额外的软件包:
golang-1.16-go golang-1.16-src golang-src
建议安装:
bzr | brz mercurial subversion
将会安装下列新软件包:
golang-1.16-go golang-1.16-src golang-go golang-src
0 升级,新安装了4 个软件包,要移除 0 个软件包,有 78 个软件包未被升级。
需下载 65.7 MB/ 安装后 377 MB的存档。
获取:1 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-1.16-src amd64 1.16.3-2longsleep1+focal [14.3 MB]
获取:2 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-1.16-go amd64 1.16.3-2longsleep1+focal [51.4 MB]
获取:3 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-src amd64 2:1.16-1longsleep1+bionic [3,952 B]
获取:4 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-go amd64 2:1.16-1longsleep1+bionic [23.0 kB]
已获取 65.7 MB,在 4 秒内(16.3 MB/s)。
正在选择未选择的软件包 golang-1.16-src。
(正在读取数据库… 目前已安装了 160983 个文件和目录。)
准备解包 …/golang-1.16-src_1.16.3-2longsleep1+focal_amd64.deb 。
正在解包 golang-1.16-src (1.16.3-2longsleep1+focal) …
正在选择未选择的软件包 golang-1.16-go。
准备解包 …/golang-1.16-go_1.16.3-2longsleep1+focal_amd64.deb 。
正在解包 golang-1.16-go (1.16.3-2longsleep1+focal) …
正在选择未选择的软件包 golang-src。
准备解包 …/golang-src_2%3a1.16-1longsleep1+bionic_amd64.deb 。
正在解包 golang-src (2:1.16-1longsleep1+bionic) …
正在选择未选择的软件包 golang-go。
准备解包 …/golang-go_2%3a1.16-1longsleep1+bionic_amd64.deb …
正在解包 golang-go (2:1.16-1longsleep1+bionic) …
正在设置 golang-1.16-src (1.16.3-2longsleep1+focal) …
正在设置 golang-1.16-go (1.16.3-
测试Go语言
如果在上面的部分,安装Go语言的内核没有出错,那么请在浏览器中重新加载页面,切换到Go语言的内核并使用常见的Go语言语法来测试Go程序。
import "fmt"
fmt.Println("Hello, Gianni!")
for i := 1; i < 10; i++ {
print(i)
}
检查当前的初始状态,是指环境变量吗?
$go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.16"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.16/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.3"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1038342927=/tmp/go-build -gno-record-gcc-switches"
虽然很重要,但在代码单元格中需要使用美元符号$来执行bash命令。
举个例子,
$go get github.com/kkdai/youtube/v2
$go env GOPATH
$dir /root/go/
$pwd
对于Python内核,我记得有”!”、”%”、”%%”和”%%bash”等选项。
### Alternatively: Using "jupyter kernel install <kernel-dir>"
# ! mkdir -p ./gophernotes
# ! cp $REPO_LOCAL/kernel ./gophernotes
# ! jupyter kernel install ./gophernotes
请注意以下事项限制:
https://github.com/gopherdata/gophernotes#限制
如果您想了解其他内核,请尝试查询有关jupyter内核的信息。