golang

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

次のリビジョン
前のリビジョン
golang [2019/09/17 12:17] – 作成 ともやんgolang [2023/07/17 09:18] (現在) – [Go のワークスペース (GOPATH 環境変数)] ともやん
行 1: 行 1:
 <html> <html>
   <style>   <style>
-    #go-logo img +    #go-logo iframe 
-      display: block;+      /*display: block;
       height: 10rem;       height: 10rem;
       max-height: 200px;       max-height: 200px;
-      width: 250px;+      width: 250px;*/
       margin: 0 auto;       margin: 0 auto;
       object-fit: cover;       object-fit: cover;
行 13: 行 13:
       min-width: 200px;       min-width: 200px;
       overflow:hidden;       overflow:hidden;
 +    }
 +    #clearfix:after {
 +      content: "";
 +      display: block;
 +      height: 0;
 +      visibility: hidden;
 +      clear: both;
 +    }
 +    #clearfix {
 +      display: inline-table;
 +    }
 +    /* Hides from IE-mac \*/
 +    * html #clearfix {
 +      height: 1%;
 +    }
 +    #clearfix {
 +      display: block;
     }     }
   </style>   </style>
 </html> </html>
 ====== Go 関連 ====== ====== Go 関連 ======
-<WRAP multi-columns> +本家: [[https://golang.org/|The Go Programming Language]]\\ 
-<WRAP left #go-logo> +ソースコード: [[https://github.com/golang/go|golang/go: The Go programming language]]\\ 
-{{:golang:golang_logo.png|Go Logo}}\\ +<WRAP multi-columns #clearfix
-</WRAP+<WRAP youtube #go-logo><html
-<WRAP #desc>+  <iframe width="300" height="240" src="https://www.youtube.com/embed/n8F55puGHIs" frameborder="0"  
 +    allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe
 +</html></WRAP>
 Go はプログラミング言語の 1 つである。(コンパイラー言語) 2009 年、Google で Robert Griesemer、ロブ・パイク、ケン・トンプソンによって設計された。Go は、静的型付け、C言語の伝統に則ったコンパイル言語、メモリ安全性、ガベージコレクション、構造的型付け、CSP スタイルの並行性などの特徴を持つ。Go のコンパイラ、ツール、およびソースコードは、すべてフリーかつオープンソースである。\\ Go はプログラミング言語の 1 つである。(コンパイラー言語) 2009 年、Google で Robert Griesemer、ロブ・パイク、ケン・トンプソンによって設計された。Go は、静的型付け、C言語の伝統に則ったコンパイル言語、メモリ安全性、ガベージコレクション、構造的型付け、CSP スタイルの並行性などの特徴を持つ。Go のコンパイラ、ツール、およびソースコードは、すべてフリーかつオープンソースである。\\
 \\ \\
 また、軽量スレッディングのための機能、Python のような動的型付け言語のようなプログラミングの容易性、などの特徴もある。Go 処理系としてはコンパイラのみが開発されている。マスコット・キャラクターは Gopher (ホリネズミ)。\\ また、軽量スレッディングのための機能、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]] より\\
 +</WRAP>
 +
 +===== インストール =====
 +公式: [[https://go.dev/doc/install|Download and install - The Go Programming Language]] [[gtr>https://go.dev/doc/install|翻訳]]\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>sudo</b> dnf install golang
 +</pre></html></WRAP>
 +<WRAP color_result_long><html><pre>
 +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    
 +依存関係が解決しました。
 +==============================================================================================================
 + パッケージ                    アーキテクチャー     バージョン                    リポジトリー          サイズ
 +==============================================================================================================
 +インストール:
 + <b class=GRN>golang                       </b> x86_64               1.20.5-1.fc38                 updates               604 k
 +依存関係のインストール:
 + <b class=GRN>golang-bin                   </b> x86_64               1.20.5-1.fc38                 updates                58 M
 + <b class=GRN>golang-src                   </b> noarch               1.20.5-1.fc38                 updates                11 M
 + <b class=GRN>libserf                      </b> x86_64               1.3.9-27.fc38                 fedora                 57 k
 + <b class=GRN>subversion-libs              </b> x86_64               1.14.2-13.fc38                fedora                1.5 M
 + <b class=GRN>utf8proc                     </b> x86_64               2.7.0-4.fc38                  fedora                 80 k
 +弱い依存関係のインストール:
 + <b class=GRN>subversion                   </b> 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/
 +  インストール中   : libserf-1.3.9-27.fc38.x86_64                                                         3/
 +  インストール中   : subversion-libs-1.14.2-13.fc38.x86_64                                                4/7 
 +  インストール中   : subversion-1.14.2-13.fc38.x86_64                                                     5/
 +  scriptletの実行中: subversion-1.14.2-13.fc38.x86_64                                                     5/
 +  インストール中   : 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/
 +  検証             : subversion-1.14.2-13.fc38.x86_64                                                     2/
 +  検証             : subversion-libs-1.14.2-13.fc38.x86_64                                                3/7 
 +  検証             : utf8proc-2.7.0-4.fc38.x86_64                                                         4/
 +  検証             : 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   
 +
 +完了しました!
 +
 +</pre></html></WRAP>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>go</b> version
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +go version go1.20.5 linux/amd64
 +</pre></html></WRAP>
 +</WRAP>
 +
 +==== 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 のワークスペースは標準で <html><code>$HOME/go</code></html> です。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>go</b> env GOPATH
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +/home/tomoyan/go
 +</pre></html></WRAP>
 +</WRAP>
 +
 +Go でインストールしたコマンドを使用するには <html><code>$(go env GOPATH)/bin</code></html> を PATH に追加する。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>mkdir</b> -p ~/go
 +<b class=GRN>$</b> <b class=HIY>echo</b> 'export GOPATH=$(go env GOPATH)' >> $HOME/.bashrc
 +<b class=GRN>$</b> <b class=HIY>echo</b> 'export PATH=$PATH:$(go env GOPATH)/bin' >> $HOME/.bashrc
 +<b class=GRN>$</b> <b class=HIY>source</b> ~/.bashrc
 +<b class=GRN>$</b> <b class=HIY>echo</b> $PATH
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +/home/tomoyan/go/bin: ...
 +</pre></html></WRAP>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>echo</b> $GOPATH
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +/home/tomoyan/go
 +</pre></html></WRAP>
 +</WRAP>
 +
 +GOPATH を変更するには以下のように設定する。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>mkdir</b> -p ~/go
 +<b class=GRN>$</b> <b class=HIY>echo</b> 'export GOPATH=$HOME/go' >> $HOME/.bashrc
 +<b class=GRN>$</b> <b class=HIY>source</b> ~/.bashrc
 +<b class=GRN>$</b> <b class=HIY>go</b> env GOPATH
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +/home/tomoyan/go
 +</pre></html></WRAP>
 +</WRAP>
 +
 +===== プログラムの作成 =====
 +公式: [[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 でプログラムを作成するには、<html><code>$(go env GOPATH)/src</code></html> サブディレクトリに .go 拡張子を持つファイルを作成する。\\
 +go.mod ファイルを作成してコードの依存関係追跡を有効にする。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>mkdir</b> <b class=HIK>-p</b> $(go env GOPATH)/src/haruo && <b class=HIY>cd</b> $(go env GOPATH)/src/haruo
 +<b class=GRN>$</b> <b class=HIY>go</b> mod init example/haruo
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +go: creating new go.mod: module example/haruo
 +go: to add module requirements and sums:
 + go mod tidy
 +
 +</pre></html></WRAP>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>cat</b> go.mod
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +gmodule example/haruo
 +
 +go 1.20
 +
 +</pre></html></WRAP>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>nano</b> haruo.go
 +</pre></html></WRAP>
 +<WRAP color_mincode><code go haruo.go>
 +package main
 +
 +import "fmt"
 +
 +func main() {
 +    fmt.Println("Haruo, World!")
 +}
 +</code></WRAP>
 +</WRAP>
 +
 +変更を保存し、同じディレクトリで haruo.go を実行する。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>go</b> run .
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +Haruo, World!
 +</pre></html></WRAP>
 +</WRAP>
 +
 +hello.go をビルドするは簡単である。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>go</b> build
 +<b class=GRN>$</b> <b class=HIY>ls</b>
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +go.mod  <b class=GRN>haruo</b>  haruo.go
 +</pre></html></WRAP>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>./haruo</b>
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +Haruo, World!
 +</pre></html></WRAP>
 +</WRAP>
 +
 +さらに **go install** で **$GOPATH/bin** にインストールされる。 \\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>go</b> install
 +<b class=GRN>$</b> <b class=HIY>ls</b> $(go env GOPATH)/bin
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +haruo
 +</pre></html></WRAP>
 +</WRAP>
 +また、**$GOPATH/pkg** のすべての依存関係を構築およびキャッシュする。\\
 +
 +プログラムは **$GOPATH/bin** にインストールされるため、一般的には PATH 環境変数に追加して、フルパスを指定せずに Go プログラムを実行できるようにする。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>echo</b> 'export PATH=$PATH:$(go env GOPATH)/bin' >> $HOME/.bashrc
 +<b class=GRN>$</b> <b class=HIY>source</b> ~/.bashrc
 +<b class=GRN>$</b> <b class=HIY>haruo</b>
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +Haruo, World!
 +</pre></html></WRAP>
 +</WRAP>
 +
 +===== Go パッケージのインストール =====
 +Go パッケージは **go install** コマンドでインストールできる。\\
 +
 +==== gotop ====
 +<WRAP #gotop_video>
 +{{:golang:gotop_001.mp4?550x330|gotop}}\\
 +</WRAP>
 +<html><script>
 +  const video = document.getElementById('gotop_video').getElementsByTagName('video')[0];
 +  video.setAttribute('autoPlay','true');
 +  video.setAttribute('loop','true');
 +  video.addEventListener("canplay", function() {
 +    setTimeout(function() {
 +      video.play();
 +    }, 1);
 +  });
 +</script></html>
 +[[git>cjbassi/gotop|cjbassi/gotop: A terminal based graphical activity monitor inspired by gtop and vtop]]\\
 +\\
 +Go 1.17 から <html><code>go get</code></html> でのインストールが非推奨になった🤔\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>go</b> get github.com/cjbassi/gotop
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +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'.
 +
 +</pre></html></WRAP>
 +</WRAP>
 +
 +<html><code>go install</code></html> でインストールする。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>go</b> install github.com/cjbassi/gotop@latest
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +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
 +
 +</pre></html></WRAP>
 </WRAP> </WRAP>
 +
 +gotop を実行する。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>gotop</b>
 +</pre></html></WRAP>
 </WRAP> </WRAP>
 +
 +===== 参考文献 =====
 +[[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]]\\
  
  • golang.1568690236.txt.gz
  • 最終更新: 2019/09/17 12:17
  • by ともやん