前言
有一次,在逛 youtube
時(shí),看到有個(gè)播主直接在文檔上運(yùn)行代碼片段。
這把我一下子整蒙了,這是什么神操作?
一番搜索之后,知道了這是 Google
的一個(gè)服務(wù) Colaboratory
。
Colaboratory
先附上地址:傳送門。
主要用于機(jī)器學(xué)習(xí)方面,可以直接運(yùn)行 Python
代碼。
當(dāng)然,我們也可以用于編寫教程。想想,邊分享,邊執(zhí)行代碼是多么爽(裝逼)的事情。
運(yùn)行Go程序
言歸正傳,并不是所有人都會(huì)或都需要寫 Python
,那我們可以安裝自己需要的語(yǔ)言,當(dāng)然,寫法上還是有一些變通的。
創(chuàng)建筆記本

安裝并運(yùn)行
1、添加一個(gè)代碼片段,并下載我們需要的版本

具體代碼如下:
# 初始化Golang環(huán)境
%cd /tmp
!wget -c https://golang.google.cn/dl/go1.16.6.linux-amd64.tar.gz -O go.tar.gz
!tar -xz -C /usr/local -f go.tar.gz
!rm -rf go.tar.gz
%cd /content
!mkdir -p go/src go/bin
2、設(shè)置 Golang
環(huán)境變量

具體代碼如下:
# 使用Python修改環(huán)境變量
import os
os.environ["PATH"] += ":/usr/local/go/bin:/content/go/bin"
os.environ["GOPATH"] = "/content/go"
3、創(chuàng)建項(xiàng)目

具體代碼如下:
%cd /content/go/src
!mkdir -p gitlab.demo.com/crmgo/php
%cd gitlab.demo.com/crmgo/php
4、編寫 main.go

具體代碼如下:
# 初始化main.go
%%writefile main.go
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
5、項(xiàng)目初始化

具體代碼如下:
!go mod init
!go mod tidy
6、執(zhí)行程序

具體代碼如下:
運(yùn)行代碼
自上而下的運(yùn)行,就行了


一些不算友好的現(xiàn)狀
- 需要自備梯子
- 會(huì)話會(huì)在一定時(shí)間之后停止,所以我們的環(huán)境不會(huì)保存。
- 不能像執(zhí)行
Python
程序那樣,直接寫代碼片段就行,每次都需要寫入文件,再執(zhí)行。
- 當(dāng)然,網(wǎng)上也有一些解決方案,但并不是很友好,畢竟人家本意還是給我們執(zhí)行
Python
代碼的。
- 每條命令都需要加上
!
,或者有一些申明。
- 修改環(huán)境變量是個(gè)頭疼的問題,需要寫
Python
代碼。
到此這篇關(guān)于在Colaboratory上運(yùn)行Go程序的文章就介紹到這了,更多相關(guān)Colaboratory運(yùn)行Go程序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Go語(yǔ)言編譯程序從后臺(tái)運(yùn)行,不出現(xiàn)dos窗口的操作
- 解決運(yùn)行django程序出錯(cuò)問題 ''str''object has no attribute''_meta''
- 服務(wù)器端Go程序?qū)﹂L(zhǎng)短鏈接的處理及運(yùn)行參數(shù)的保存
- 在Apache服務(wù)器上同時(shí)運(yùn)行多個(gè)Django程序的方法