====== Go言語 ====== {{ :golang:go-gopher-svgrepo-com.svg |Go Gopher SVG Vector}}\\ [[https://www.svgrepo.com/svg/373635/go-gopher|Go Gopher Vector SVG Icon - SVG Repo]] より\\ 本家: [[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 -y golang
リポジトリの更新を読み込み中:
リポジトリを読み込みました。
Package                              Arch       Version                               Repository              Size
Installing:
 golang                              x86_64     1.23.6-1.fc41                         updates              8.9 MiB
Installing dependencies:
 go-filesystem                       x86_64     3.6.0-5.fc41                          updates              0.0   B
 golang-bin                          x86_64     1.23.6-1.fc41                         updates            113.9 MiB
 golang-src                          noarch     1.23.6-1.fc41                         updates             76.3 MiB
Installing weak dependencies:
 mercurial                           x86_64     6.8.2-1.fc41                          updates             30.8 MiB

Transaction Summary:
 Installing:         5 packages

パッケージサイズ 47 MiB 、ダウンロードサイズ 47 MiB 。
完了後、230 MiB のサイズが利用されます(インストール 230 MiB、削除 0 B)。
[1/5] golang-0:1.23.6-1.fc41.x86_64                                       100% | 336.0 KiB/s | 668.9 KiB |  00m02s
[2/5] go-filesystem-0:3.6.0-5.fc41.x86_64                                 100% |  38.6 KiB/s |   9.0 KiB |  00m00s
[3/5] golang-src-0:1.23.6-1.fc41.noarch                                   100% | 762.2 KiB/s |  12.8 MiB |  00m17s
[4/5] mercurial-0:6.8.2-1.fc41.x86_64                                     100% | 390.4 KiB/s |   6.5 MiB |  00m17s
[5/5] golang-bin-0:1.23.6-1.fc41.x86_64                                   100% | 915.6 KiB/s |  27.5 MiB |  00m31s
------------------------------------------------------------------------------------------------------------------
[5/5] Total                                                               100% |   1.5 MiB/s |  47.4 MiB |  00m32s
トランザクションを実行中
[1/7] パッケージ ファイルを検証                               100% |   7.0   B/s |   5.0   B |  00m01s
[2/7] トランザクションの準備                                   100% |   0.0   B/s |   5.0   B |  00m08s
[3/7] インストール中 go-filesystem-0:3.6.0-5.fc41.x86_64           100% | 876.0   B/s | 392.0   B |  00m00s
[4/7] インストール中 golang-src-0:1.23.6-1.fc41.noarch             100% |  21.8 MiB/s |  77.2 MiB |  00m04s
[5/7] インストール中 golang-bin-0:1.23.6-1.fc41.x86_64             100% |  64.9 MiB/s | 113.9 MiB |  00m02s
[6/7] インストール中 golang-0:1.23.6-1.fc41.x86_64                 100% |  25.2 MiB/s |   8.9 MiB |  00m00s
[7/7] インストール中 mercurial-0:6.8.2-1.fc41.x86_64               100% |   1.4 MiB/s |  31.1 MiB |  00m22s
完了しました!
$ go version
go version go1.23.6 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
$ cat << 'EOF' >> ~/.commonshrc
# go initialize
export GOPATH=$(go env GOPATH)
export PATH=$PATH:$(go env GOPATH)/bin
EOF
$ . ~/.bashrc
$ echo $GOPATH
/home/tomoyan/go/bin: ...
$ echo $GOPATH
/home/tomoyan/go
GOPATH を変更するには以下のように設定する。\\
$ mkdir -p ~/.go
$ cat << 'EOF' >> ~/.commonshrc
# go initialize
export GOPATH=$HOME/.go       
export PATH=$PATH:$(go env GOPATH)/bin
EOF
$ . ~/.bashrc
$ echo $GOPATH
$ 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
$ cat go.mod
module example/haruo

go 1.23.6
$ cat << 'EOF' > haruo.go
    package main

    import "fmt"

    func main() {
        fmt.Println("Haruo, World!")
    }
EOF
変更を保存し、同じディレクトリで haruo.go を実行する。\\
$ go run .
Haruo, World!
hello.go をビルドするは簡単である。\\
$ go build
$ ll
合計 2092
-rw-r--r-- 1 tomoyan tomoyan      32  3月  5 10:31 go.mod
-rwxr-xr-x 1 tomoyan tomoyan 2130671  3月  5 10:42 haruo
-rw-r--r-- 1 tomoyan tomoyan     107  3月  5 10:32 haruo.go
$ ./haruo
Haruo, World!
さらに **go install** で **$GOPATH/bin** にインストールされる。 \\
$ go install
$ ll $(go env GOPATH)/bin
合計 2084
-rwxr-xr-x 1 tomoyan tomoyan 2130671  3月  5 10:33 haruo
また、**$GOPATH/pkg** のすべての依存関係を構築およびキャッシュする。\\ プログラムは **$GOPATH/bin** にインストールされるため、一般的には PATH 環境変数に追加して、フルパスを指定せずに Go プログラムを実行できるようにする。\\
$ echo 'export PATH=$PATH:$(go env GOPATH)/bin' >> ~/.bashrc
$ . ~/.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]]\\ [[git>xxxserxxx/gotop|GitHub - xxxserxxx/gotop: A terminal based graphical activity monitor inspired by gtop and vtop]]\\ \\ go install でインストールする。\\
$ go install github.com/xxxserxxx/gotop/v4/cmd/gotop@latest
go: downloading github.com/xxxserxxx/gotop/v4 v4.2.0
go: downloading github.com/VictoriaMetrics/metrics v1.18.1
go: downloading github.com/gizak/termui/v3 v3.1.0
go: downloading github.com/droundy/goopt v0.0.0-20220217183150-48d6390ad4d1
go: downloading github.com/cloudfoundry-attic/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21
go: downloading github.com/shibukawa/configdir v0.0.0-20170330084843-e180dbdc8da0
go: downloading github.com/xxxserxxx/lingo/v2 v2.0.1
go: downloading github.com/anatol/smart.go v0.0.0-20220917195147-c0b00d90f8cc
go: downloading github.com/jaypipes/ghw v0.9.0
go: downloading github.com/shirou/gopsutil v3.20.12+incompatible
go: downloading github.com/VividCortex/ewma v1.2.0
go: downloading github.com/distatus/battery v0.10.0
go: downloading github.com/mattn/go-runewidth v0.0.13
go: downloading github.com/mitchellh/go-wordwrap v1.0.1
go: downloading github.com/nsf/termbox-go v1.1.1
go: downloading github.com/valyala/histogram v1.2.0
go: downloading github.com/BurntSushi/toml v1.1.0
go: downloading golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41
go: downloading github.com/valyala/fastrand v1.1.0
go: downloading github.com/rivo/uniseg v0.2.0
go: downloading github.com/ghodss/yaml v1.0.0
go: downloading github.com/jaypipes/pcidb v1.0.0
go: downloading gopkg.in/yaml.v2 v2.4.0
go: downloading github.com/mitchellh/go-homedir v1.1.0
gotop を実行する😉\\
$ gotop
gotop を NVIDIA サポートを有効化して実行するには、''--nvidia'' オプションを付ける😍\\
$ gotop --nvidia
使い方\\
$ gotop --help
Usage of gotop:
	A terminal based graphical activity monitor, inspired by gtop and vtop
Options:
  --remote-name=        Remote: name of remote gotop
  --remote-url=         Remote: URL of remote gotop
  --remote-refresh=     Remote: Frequency to refresh data, in seconds
  -c, --color=default   Set a colorscheme.
  -S, --graphscale=7    Graph scale factor, >0
  -v, -V, --version     Print version and exit.
  -p, --percpu          Show each CPU in the CPU widget.
  --no-percpu           Show aggregate CPU in the CPU widget.
  -a, --averagecpu      Show average CPU in the CPU widget.
  --no-averagecpu       Disable show average CPU in the CPU widget.
  --fahrenheit          Show temperatures in fahrenheit.
  --celsius             Show temperatures in celsius.
  -s, --statusbar       Show a statusbar with the time.
  --no-statusbar        Disable statusbar.
  -r, --rate=1s         Refresh frequency. Most time units accepted.  "1m" = refresh every minute.  "100ms" = refresh every 100ms.
  -l, --layout=default  Name of layout spec file for the UI. Use "-" to pipe.
  -i, --interface=all   Select network interface. Several interfaces can be defined using comma separated values. Interfaces can also be ignored using "!"
  -x, --export=         Enable metrics for export on the specified port.
  --mbps                Show network rate as mbps.
  --bytes               args.no-mbps
  --test                Runs tests and exits with success/failure code.
  --no-test             Disable tests.
  -C                    Config file to use instead of default (MUST BE FIRST ARGUMENT).
  --nvidia              Enable NVidia GPU metrics.
  --no-nvidia           Disable NVidia GPU metrics.
  --list=               List <devices|layouts|colorschemes|paths|keys|langs>
      devices: Prints out device names for filterable widgets
      layouts: Lists built-in layouts
      colorschemes: Lists built-in colorschemes
      paths: List out configuration file search paths
      widgets: Widgets that can be used in a layout
      keys: Show the keyboard bindings.
      langs: Show supported language translations.
  --write-config  Write out a default config file.
  -h, --help      Show usage message
  --version       Show version

Go 1.17 から go get でのインストールが非推奨になった🤔\\
$ go get github.com/xxxserxxx/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'.
===== 参考文献 ===== [[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]]\\