golang

差分

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

この比較画面へのリンク

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
golang [2023/07/17 06:38] – [Go 関連] ともやん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;
行 36: 行 36:
 本家: [[https://golang.org/|The Go Programming Language]]\\ 本家: [[https://golang.org/|The Go Programming Language]]\\
 ソースコード: [[https://github.com/golang/go|golang/go: The Go programming language]]\\ ソースコード: [[https://github.com/golang/go|golang/go: The Go programming language]]\\
-<WRAP youtube><html>+<WRAP multi-columns #clearfix> 
 +<WRAP youtube #go-logo><html>
   <iframe width="300" height="240" src="https://www.youtube.com/embed/n8F55puGHIs" frameborder="0"    <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>     allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
-</html></WRAP>\\+</html></WRAP>
 Go はプログラミング言語の 1 つである。(コンパイラー言語) 2009 年、Google で Robert Griesemer、ロブ・パイク、ケン・トンプソンによって設計された。Go は、静的型付け、C言語の伝統に則ったコンパイル言語、メモリ安全性、ガベージコレクション、構造的型付け、CSP スタイルの並行性などの特徴を持つ。Go のコンパイラ、ツール、およびソースコードは、すべてフリーかつオープンソースである。\\ Go はプログラミング言語の 1 つである。(コンパイラー言語) 2009 年、Google で Robert Griesemer、ロブ・パイク、ケン・トンプソンによって設計された。Go は、静的型付け、C言語の伝統に則ったコンパイル言語、メモリ安全性、ガベージコレクション、構造的型付け、CSP スタイルの並行性などの特徴を持つ。Go のコンパイラ、ツール、およびソースコードは、すべてフリーかつオープンソースである。\\
 \\ \\
行 140: 行 141:
 </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_term>
 <WRAP color_command><html><pre> <WRAP color_command><html><pre>
行 154: 行 175:
  
 ===== プログラムの作成 ===== ===== プログラムの作成 =====
-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
  
行 165: 行 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>
  
 ===== 参考文献 ===== ===== 参考文献 =====
  • golang.1689543511.txt.gz
  • 最終更新: 2023/07/17 06:38
  • by ともやん