====== Go 関連 ====== 本家: [[https://golang.org/|The Go Programming Language]]\\ ソースコード: [[https://github.com/golang/go|golang/go: The Go programming language]]\\ Go はプログラミング言語の 1 つである。(コンパイラー言語) 2009 年、Google で Robert Griesemer、ロブ・パイク、ケン・トンプソンによって設計された。Go は、静的型付け、C言語の伝統に則ったコンパイル言語、メモリ安全性、ガベージコレクション、構造的型付け、CSP スタイルの並行性などの特徴を持つ。Go のコンパイラ、ツール、およびソースコードは、すべてフリーかつオープンソースである。\\ \\ また、軽量スレッディングのための機能、Python のような動的型付け言語のようなプログラミングの容易性、などの特徴もある。Go 処理系としてはコンパイラのみが開発されている。マスコット・キャラクターは Gopher (ホリネズミ)。\\ [[https://ja.wikipedia.org/wiki/Go_(%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E)|Go (プログラミング言語) - Wikipedia]] より\\ ===== インストール ===== 公式: [[https://go.dev/doc/install|Download and install - The Go Programming Language]] [[gtr>https://go.dev/doc/install|翻訳]]\\
$ sudo dnf install golang
Fedora 38 - x86_64 - Updates                                                  6.7 kB/s | 6.1 kB     00:00    
Fedora 38 - x86_64 - Updates                                                  467 kB/s | 2.3 MB     00:05    
Fedora Modular 38 - x86_64 - Updates                                           11 kB/s | 5.2 kB     00:00    
依存関係が解決しました。
==============================================================================================================
 パッケージ                    アーキテクチャー     バージョン                    リポジトリー          サイズ
==============================================================================================================
インストール:
 golang                        x86_64               1.20.5-1.fc38                 updates               604 k
依存関係のインストール:
 golang-bin                    x86_64               1.20.5-1.fc38                 updates                58 M
 golang-src                    noarch               1.20.5-1.fc38                 updates                11 M
 libserf                       x86_64               1.3.9-27.fc38                 fedora                 57 k
 subversion-libs               x86_64               1.14.2-13.fc38                fedora                1.5 M
 utf8proc                      x86_64               2.7.0-4.fc38                  fedora                 80 k
弱い依存関係のインストール:
 subversion                    x86_64               1.14.2-13.fc38                fedora                1.0 M

トランザクションの概要
==============================================================================================================
インストール  7 パッケージ

ダウンロードサイズの合計: 72 M
インストール後のサイズ: 214 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
(1/7): libserf-1.3.9-27.fc38.x86_64.rpm                                       141 kB/s |  57 kB     00:00    
(2/7): utf8proc-2.7.0-4.fc38.x86_64.rpm                                       240 kB/s |  80 kB     00:00    
(3/7): subversion-1.14.2-13.fc38.x86_64.rpm                                   529 kB/s | 1.0 MB     00:01    
(4/7): subversion-libs-1.14.2-13.fc38.x86_64.rpm                              543 kB/s | 1.5 MB     00:02    
(5/7): golang-1.20.5-1.fc38.x86_64.rpm                                        212 kB/s | 604 kB     00:02    
(6/7): golang-src-1.20.5-1.fc38.noarch.rpm                                    2.0 MB/s |  11 MB     00:05    
(7/7): golang-bin-1.20.5-1.fc38.x86_64.rpm                                    2.5 MB/s |  58 MB     00:23    
--------------------------------------------------------------------------------------------------------------
合計                                                                          2.7 MB/s |  72 MB     00:26     
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  scriptletの実行中: golang-1.20.5-1.fc38.x86_64                                                          1/1 
  準備             :                                                                                      1/1 
  インストール中   : golang-src-1.20.5-1.fc38.noarch                                                      1/7 
  インストール中   : utf8proc-2.7.0-4.fc38.x86_64                                                         2/7 
  インストール中   : libserf-1.3.9-27.fc38.x86_64                                                         3/7 
  インストール中   : subversion-libs-1.14.2-13.fc38.x86_64                                                4/7 
  インストール中   : subversion-1.14.2-13.fc38.x86_64                                                     5/7 
  scriptletの実行中: subversion-1.14.2-13.fc38.x86_64                                                     5/7 
  インストール中   : golang-1.20.5-1.fc38.x86_64                                                          6/7 
  インストール中   : golang-bin-1.20.5-1.fc38.x86_64                                                      7/7 
  scriptletの実行中: golang-bin-1.20.5-1.fc38.x86_64                                                      7/7 
  検証             : libserf-1.3.9-27.fc38.x86_64                                                         1/7 
  検証             : subversion-1.14.2-13.fc38.x86_64                                                     2/7 
  検証             : subversion-libs-1.14.2-13.fc38.x86_64                                                3/7 
  検証             : utf8proc-2.7.0-4.fc38.x86_64                                                         4/7 
  検証             : golang-1.20.5-1.fc38.x86_64                                                          5/7 
  検証             : golang-bin-1.20.5-1.fc38.x86_64                                                      6/7 
  検証             : golang-src-1.20.5-1.fc38.noarch                                                      7/7 

インストール済み:
  golang-1.20.5-1.fc38.x86_64     golang-bin-1.20.5-1.fc38.x86_64     golang-src-1.20.5-1.fc38.noarch         
  libserf-1.3.9-27.fc38.x86_64    subversion-1.14.2-13.fc38.x86_64    subversion-libs-1.14.2-13.fc38.x86_64   
  utf8proc-2.7.0-4.fc38.x86_64   

完了しました!

$ go version
go version go1.20.5 linux/amd64
==== Go のワークスペース (GOPATH 環境変数) ==== 公式: [[https://go.dev/doc/gopath_code|How to Write Go Code (with GOPATH) - The Go Programming Language]] [[gtr>https://go.dev/doc/gopath_code|翻訳]]\\ Go のワークスペースは標準で $HOME/go です。\\
$ go env GOPATH
/home/tomoyan/go
Go でインストールしたコマンドを使用するには $(go env GOPATH)/bin を PATH に追加する。\\
$ mkdir -p ~/go
$ echo 'export GOPATH=$(go env GOPATH)' >> $HOME/.bashrc
$ echo 'export PATH=$PATH:$(go env GOPATH)/bin' >> $HOME/.bashrc
$ source ~/.bashrc
$ echo $PATH
/home/tomoyan/go/bin: ...
$ echo $GOPATH
/home/tomoyan/go
GOPATH を変更するには以下のように設定する。\\
$ mkdir -p ~/go
$ echo 'export GOPATH=$HOME/go' >> $HOME/.bashrc
$ source ~/.bashrc
$ go env GOPATH
/home/tomoyan/go
===== プログラムの作成 ===== 公式: [[https://go.dev/doc/tutorial/getting-started|Tutorial: Get started with Go - The Go Programming Language]] [[gtr>https://go.dev/doc/tutorial/getting-started|翻訳]]\\ \\ Go でプログラムを作成するには、$(go env GOPATH)/src サブディレクトリに .go 拡張子を持つファイルを作成する。\\ go.mod ファイルを作成してコードの依存関係追跡を有効にする。\\
$ mkdir -p $(go env GOPATH)/src/haruo && cd $(go env GOPATH)/src/haruo
$ go mod init example/haruo
go: creating new go.mod: module example/haruo
go: to add module requirements and sums:
	go mod tidy

$ cat go.mod
gmodule example/haruo

go 1.20

$ nano haruo.go
package main import "fmt" func main() { fmt.Println("Haruo, World!") }
変更を保存し、同じディレクトリで haruo.go を実行する。\\
$ go run .
Haruo, World!
hello.go をビルドするは簡単である。\\
$ go build
$ ls
go.mod  haruo  haruo.go
$ ./haruo
Haruo, World!
さらに **go install** で **$GOPATH/bin** にインストールされる。 \\
$ go install
$ ls $(go env GOPATH)/bin
haruo
また、**$GOPATH/pkg** のすべての依存関係を構築およびキャッシュする。\\ プログラムは **$GOPATH/bin** にインストールされるため、一般的には PATH 環境変数に追加して、フルパスを指定せずに Go プログラムを実行できるようにする。\\
$ echo 'export PATH=$PATH:$(go env GOPATH)/bin' >> $HOME/.bashrc
$ source ~/.bashrc
$ haruo
Haruo, World!
===== Go パッケージのインストール ===== Go パッケージは **go install** コマンドでインストールできる。\\ ==== gotop ==== {{:golang:gotop_001.mp4?550x330|gotop}}\\ [[git>cjbassi/gotop|cjbassi/gotop: A terminal based graphical activity monitor inspired by gtop and vtop]]\\ \\ Go 1.17 から go get でのインストールが非推奨になった🤔\\
$ go get github.com/cjbassi/gotop
go: go.mod file not found in current directory or any parent directory.
	'go get' is no longer supported outside a module.
	To build and install a command, use 'go install' with a version,
	like 'go install example.com/cmd@latest'
	For more information, see https://golang.org/doc/go-get-install-deprecation
	or run 'go help get' or 'go help install'.

go install でインストールする。\\
$ go install github.com/cjbassi/gotop@latest
go: downloading github.com/cjbassi/gotop v0.0.0-20200829004927-65d76af83079
go: downloading github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815
go: downloading github.com/gizak/termui/v3 v3.0.0
go: downloading github.com/distatus/battery v0.9.0
go: downloading github.com/shirou/gopsutil v2.18.11+incompatible
go: downloading github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd
go: downloading github.com/mattn/go-runewidth v0.0.4
go: downloading github.com/mitchellh/go-wordwrap v1.0.0
go: downloading github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d
go: downloading golang.org/x/sys v0.0.0-20190116161447-11f53e031339

gotop を実行する。\\
$ gotop
===== 参考文献 ===== [[https://developer.fedoraproject.org/tech/languages/go/go-installation.html|Go — Fedora Developer Portal]]\\ [[https://developer.fedoraproject.org/tech/languages/go/go-programs.html|Writing Go programs — Fedora Developer Portal]]\\ [[https://www.paloaltonetworks.jp/company/in-the-news/2019/the-gopher-in-the-room-analysis-of-golang-malware-in-the-wild|Gopher の人気度は: インターネット上の GoLang マルウェア分析 - Palo Alto Networks]]\\ **Grumpy**\\ [[https://opensource.googleblog.com/2017/01/grumpy-go-running-python.html|Grumpy: Go running Python! | Google Open Source Blog]]\\ [[https://qiita.com/e-a-st/items/998250b3d430d82594c2|Grumpy(Go running Python)を試してみた。 - Qiita]]\\ [[https://japan.zdnet.com/article/35094636/|グーグル、Pythonコードを「Go」言語に変換する「Grumpy」を公開 - ZDNet Japan]]\\ ==== 付録 ==== [[tw>tomoyan596/status/1375684357428404228|「git の shallow clone で高速に clone しつつ、バックグランドで unshallow してくれる go で書かれたツールが存在しますねぇ🤔なるほど...」 / Twitter]]\\