golang

差分

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

この比較画面へのリンク

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
golang [2021/03/27 15:04] – [参考文献] ともやん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;
行 37: 行 37:
 ソースコード: [[https://github.com/golang/go|golang/go: The Go programming language]]\\ ソースコード: [[https://github.com/golang/go|golang/go: The Go programming language]]\\
 <WRAP multi-columns #clearfix> <WRAP multi-columns #clearfix>
-<WRAP left column #go-logo> +<WRAP youtube #go-logo><html
-{{:golang:golang_logo.png|Go Logo}}\\ +  <iframe width="300" height="240" src="https://www.youtube.com/embed/n8F55puGHIs" frameborder="0"  
-</WRAP>+    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 のコンパイラ、ツール、およびソースコードは、すべてフリーかつオープンソースである。\\
 \\ \\
行 47: 行 48:
  
 ===== インストール ===== ===== インストール =====
-Fedora では最新版の Go がインストールされる。\\ +公式: [[https://go.dev/doc/install|Download and install - The Go Programming Language]] [[gtr>https://go.dev/doc/install|翻訳]]\\ 
-2019/09/17 現在の Go の最新バージョンは v1.12.9 である。\\ +<WRAP color_term> 
-Fedora Linux on WSL 2\\ +<WRAP color_command><html><pre> 
-<code+<b class=GRN>$</b> <b class=HIY>sudo</b> dnf install golang 
-$ sudo dnf install golang +</pre></html></WRAP> 
-$ go version +<WRAP color_result_long><html><pre> 
-go version go1.12.9 linux/amd64 +Fedora 38 - x86_64 - Updates                                                  6.7 kB/s | 6.1 kB     00:00     
-</code>+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                     </bx86_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
  
-GOPATH 環境変数を設定\\ +トランザクションの概要 
-<code+============================================================================================================== 
-mkdir -p ~/go +インストール  7 パッケージ 
-$ echo 'export GOPATH=$HOME/go>> $HOME/.bashrc + 
-source ~/.bashrc +ダウンロードサイズの合計: 72 M 
-$ go env GOPATH+インストール後のサイズ: 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    
 + 
 +完了しました! 
 + 
 +</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 /home/tomoyan/go
-</code>+</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>
  
 ===== プログラムの作成 ===== ===== プログラムの作成 =====
-Go でプログラムを作成するには、$GOPATH/src サブディレクトリに .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|翻訳]]\\ 
-<code+\\ 
-$ mkdir -p $GOPATH/src/hello && cd $GOPATH/src/hello +Go でプログラムを作成するには、<html><code>$(go env GOPATH)/src</code></html> サブディレクトリに .go 拡張子を持つファイルを作成する。\\ 
-vi hello.go +go.mod ファイルを作成してコードの依存関係追跡を有効にする。\\ 
-</code+<WRAP color_term
-<code go hello.go>+<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 package main
  
行 77: 行 208:
  
 func main() { func main() {
-    fmt.Println("Hello, World!")+    fmt.Println("Haruo, World!")
 } }
-</code>+</code></WRAP> 
 +</WRAP>
  
-変更を保存し、同じディレクトリで hello.go を実行する。\\ +変更を保存し、同じディレクトリで haruo.go を実行する。\\ 
-<code+<WRAP color_term
-$ go run hello.go +<WRAP color_command><html><pre> 
-Hello, World! +<b class=GRN>$</b> <b class=HIY>go</b> run . 
-</code>+</pre></html></WRAP> 
 +<WRAP color_result><html><pre> 
 +Haruo, World! 
 +</pre></html></WRAP> 
 +</WRAP>
  
 hello.go をビルドするは簡単である。\\ hello.go をビルドするは簡単である。\\
-<code+<WRAP color_term
-$ go build +<WRAP color_command><html><pre> 
-$ ls +<b class=GRN>$</b> <b class=HIY>go</b> build 
-hello  hello.go +<b class=GRN>$</b> <b class=HIY>ls</b> 
-$ ./hello +</pre></html></WRAP> 
-Hello, World! +<WRAP color_result><html><pre> 
-</code>+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** にインストールされる。 \\ さらに **go install** で **$GOPATH/bin** にインストールされる。 \\
-<code+<WRAP color_term
-$ go install +<WRAP color_command><html><pre> 
-$ ls $GOPATH/bin +<b class=GRN>$</b> <b class=HIY>go</b> install 
-hello +<b class=GRN>$</b> <b class=HIY>ls</b> $(go env GOPATH)/bin 
-</code>+</pre></html></WRAP> 
 +<WRAP color_result><html><pre> 
 +haruo 
 +</pre></html></WRAP> 
 +</WRAP>
 また、**$GOPATH/pkg** のすべての依存関係を構築およびキャッシュする。\\ また、**$GOPATH/pkg** のすべての依存関係を構築およびキャッシュする。\\
  
 プログラムは **$GOPATH/bin** にインストールされるため、一般的には PATH 環境変数に追加して、フルパスを指定せずに Go プログラムを実行できるようにする。\\ プログラムは **$GOPATH/bin** にインストールされるため、一般的には PATH 環境変数に追加して、フルパスを指定せずに Go プログラムを実行できるようにする。\\
-<code+<WRAP color_term
-$ echo 'export PATH=$PATH:$GOPATH/bin' >> $HOME/.bashrc +<WRAP color_command><html><pre> 
-$ source ~/.bashrc +<b class=GRN>$</b> <b class=HIY>echo</b> 'export PATH=$PATH:$(go env GOPATH)/bin' >> $HOME/.bashrc 
-hello +<b class=GRN>$</b> <b class=HIY>source</b> ~/.bashrc 
-Hello, World! +<b class=GRN>$</b> <b class=HIY>haruo</b> 
-</code>+</pre></html></WRAP> 
 +<WRAP color_result><html><pre> 
 +Haruo, World! 
 +</pre></html></WRAP> 
 +</WRAP>
  
 ===== Go パッケージのインストール ===== ===== Go パッケージのインストール =====
 +Go パッケージは **go install** コマンドでインストールできる。\\
  
-==== アップストリームの Go パッケージ ==== +==== gotop ==== 
-アップストリームプロジェクトは **go get** コマンドでインストールできる。\\ +<WRAP #gotop_video> 
-<code+{{:golang:gotop_001.mp4?550x330|gotop}}\\ 
-$ go get github.com/gorilla/context +</WRAP> 
-</code+<html><script> 
-ゴ、ゴリラ? (gorilla)...\\+  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>
 +
 +gotop を実行する。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>gotop</b>
 +</pre></html></WRAP>
 +</WRAP>
  
 ===== 参考文献 ===== ===== 参考文献 =====
行 133: 行 336:
 [[https://japan.zdnet.com/article/35094636/|グーグル、Pythonコードを「Go」言語に変換する「Grumpy」を公開 - ZDNet Japan]]\\ [[https://japan.zdnet.com/article/35094636/|グーグル、Pythonコードを「Go」言語に変換する「Grumpy」を公開 - ZDNet Japan]]\\
  
 +==== 付録 ====
 +[[tw>tomoyan596/status/1375684357428404228|「git の shallow clone で高速に clone しつつ、バックグランドで unshallow してくれる go で書かれたツールが存在しますねぇ🤔なるほど...」 / Twitter]]\\
  
  • golang.1616825096.txt.gz
  • 最終更新: 2021/03/27 15:04
  • by ともやん