Skip to content

hikkymouse1007/Go_System_Programming

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go_System_Programming

https://www.lambdanote.com/products/go

Go By Example

https://gobyexample.com/ https://oohira.github.io/gobyexample-jp/

サンプルコード

Go by Example

https://gobyexample.com/

デバッガのセットアップ

https://qiita.com/gs1068/items/1d89d4a9bed070782298

デバッガを起動するためには、go mod でモジュールにリモートリポジトリを指定する 必要がある。 https://stackoverflow.com/questions/67306638/go-test-results-in-go-cannot-find-main-module-but-found-git-config-in-users

git init
git remote add origin https://github.com/hikkymouse1007/Go_System_Programming.git
go mod init github.com/hikkymouse1007/Go_System_Programming

https://qiita.com/ymmy02/items/b4b38a57c024510c5210

デバッグしたいファイルについて

ルートディレクトリにデバッグ対象のファイルを置いてファイルまたはデバッガを実行する

Keyword

#
# 副作用あり
#
lst1 = [1, 0, 3, 2]
lst1.sort()
lst1
# [0, 1, 2, 3] -> オブジェクトが変化したので副作用がある
#
# 副作用なし
#
lst2 = [1, 0, 3, 2]
lst3 = sorted(lst2)

lst2
# [1, 0, 3, 2] -> オブジェクトは変化していないので副作用はない

lst3
# [0, 1, 2, 3]
  • バッファ

    何か(主にデータ)を一時的にプールしておく場所 https://wa3.i-3-i.info/word11609.html

  • Go のインターフェース

    インタフェースは、構造体と違って何かしら実体を持つものを表すのでは なく、「どんなことができるか」を宣言しているだけです

main 関数について

https://jp.quora.com/Go%E8%A8%80%E8%AA%9E%E3%81%A71%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%A7%E5%AE%8C%E7%B5%90%E3%81%99%E3%82%8B%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%92%E8%A4%87%E6%95%B0%E4%BD%9C%E6%88%90%E3%81%97%E3%81%9F ファイル名を指定して実行する場合には、package mainであれば同階層ディレクトリの複数ファイルで main 関数を使っても 実行できる。

godoc のインストールについて

https://zenn.dev/mkosakana/articles/bb411e9d6b5ad9

練習問題 解答参考

https://github.com/yuyabu/go-sys

Q 2-3  補足

https://stackoverflow.com/questions/31622052/how-to-serve-up-a-json-response-using-go

// You can set your content-type header so clients know to expect json

w.Header().Set("Content-Type", "application/json")

// Another way to marshal a struct to json is to build an encoder using the http.ResponseWriter
// get a payload p := Payload{d}
json.NewEncoder(w).Encode(p)

3.4.1 補足

バッファサイズ 5 ずつ表示する

$ go run stdin.go < stdin.go
size=5 input='packa
size=5 input='ge ma
size=5 input='in

i
size=5 input='mport
size=5 input=' (
        "
size=5 input='fmt"

size=5 input='  "io"
size=5 input='
        "os
size=5 input='"
)


size=5 input='func
size=5 input='main(
size=5 input=') {

size=5 input='for {
size=5 input='
                bu
size=5 input='ffer
size=5 input=':= ma
size=5 input='ke([]
size=5 input='byte,
size=5 input=' 5)

size=5 input='  size
size=5 input=', err
size=5 input=' := o
size=5 input='s.Std
size=5 input='in.Re
size=5 input='ad(bu
size=5 input='ffer)
size=5 input='
                if
size=5 input=' err
size=5 input='== io
size=5 input='.EOF
size=5 input='{

size=5 input='fmt.P
size=5 input='rintl
size=5 input='n("EO
size=5 input='F")

size=5 input='          bre
size=5 input='ak

size=5 input='}
                f
size=5 input='mt.Pr
size=5 input='intf(
size=5 input='"size
size=5 input='=%d i
size=5 input='nput=
size=5 input=''%s\n
size=5 input='", si
size=5 input='ze, s
size=5 input='tring
size=5 input='(buff
size=5 input='er))

size=5 input='          //
size=5 input='input
size=5 input=':
                /
size=5 input='/ hel
size=5 input='lo wo
size=5 input='rld

size=5 input='  // s
size=5 input='ize=5
size=5 input=' inpu
size=5 input='t='he
size=5 input='llo

size=5 input='  // s
size=5 input='ize=5
size=5 input=' inpu
size=5 input='t=' w
size=5 input='orl

size=5 input='  // s
size=5 input='ize=2
size=5 input=' inpu
size=5 input='t='d

size=5 input='  }
}

EOF
}

Go の io パッケージ解説

https://christina04.hatenablog.com/entry/golang-io-package-diagrams

io.Copy は io.Reader からデータを読み込み、io.Writer へと書き出します。 入力と出力をそのままセットで実行する

func Copy(dst Writer, src Reader) (written int64, err error) {
	return copyBuffer(dst, src, nil)
}

エンディアンとは

https://qiita.com/tobira-code/items/a03f39a02678d80bbd26

https://wa3.i-3-i.info/word11426.html http://tanehp.ec-net.jp/heppoko-lab/prog/zakki/byteorder/byteorder.html

https://rainbow-engine.com/little-endian-big-endian/

TCP/IP プロトコルで使用されている事から、インターネット上でデータを転送する際には「ビッグエンディアン」に変換する必要があるという意味になります。 逆にリトルエンディアンは、前述の計算処理における利点がある事から Intel 等のプロセッサで利用されています

png のチャンク

スクリーンショット 2022-05-02 0 56 21 スクリーンショット 2022-05-02 0 56 28

tee コマンド補足

https://atmarkit.itmedia.co.jp/ait/articles/1611/16/news022.html

ind /bin /usr/bin -type f | tee cmdlist | less

(findコマンドの結果をファイル「cmdlist」に保存し、さらにlessコマンドで閲覧する)

Go routine & channel

並行実行と並列実行の違い https://zenn.dev/hsaki/books/golang-concurrency/viewer/term スクリーンショット 2022-05-21 15 00 54

goroutine とチャネル https://qiita.com/taigamikami/items/fc798cdd6a4eaf9a7d5e

チャネルは、並行実行される goroutine 間を接続するパイプ(トンネル)のイメージ。 つまり、並行実行している関数から値を受信する。(ある goroutine から別の goroutine へ値を渡す。) スクリーンショット 2022-05-21 15 00 54

バッファが詰まるとチャネルへの送信をブロックする。バッファが空のときは、チャネルの受信をブロックする。

バッファつきチャネルについて https://free-engineer.life/golang-buffer-channel/

package main

import (
	"fmt"
	"time"
)

func main() {
	ch := make(chan int, 3)
	go func() {
		defer close(ch)
		for i := 0; i < 5; i++ {
			ch <- i
			fmt.Printf("sent %d\n", i)
		}
	}()
	time.Sleep(3 * time.Second) // 出力をわかりやすくすため、スリープ
	for v := range ch {
		fmt.Printf("received value: %d\n", v)
		time.Sleep(1 * time.Second) // 出力をわかりやすくすため、スリープ
	}
}

  1. バッファサイズ 3 のチャネルを作る。
  2. [0, 1, 2] の 3 つの値を貯める。(buffer: 3 なので) for は i = 2 で処理がブロックされる(値が入らないから)
[] <- 0, 1, 2
↓
[0, 1, 2]
  1. 3 秒まつ
  2. range により次の処理が実行開始
  3. range によって v = 0 を受け取る
  4. バッファに空きが 1 できたので go func で処理を再開(i = 3 から), 3 を追加して満杯になる、処理をブロック
  5. v は channel の range から次の値を受け取る(v = 1)
  6. バッファに空きが 1 できたので go func で処理を再開(i = 4 から), 4 を追加して満杯になるが処理が終わったのでクローズ
  7. 溜まったバッファサイズ 3 の、 2,3,4 を全て v が受け取ってフィニッシュ
  <- 0 [1, 2]
 [1, 2] <- 3 (i = 3)
  <- 1 [2, 3]
[2, 3] <- 4 (i = 4)
[2, 3, 4]
 <- 2, 3, 4 []
sent 0
sent 1
sent 2             // ← ここでチャネルは一杯。チャネルが一杯になるまでは、受信操作が実行されなくても送信できる
received value: 0  // ← 1つ受信した(取り出した)ので、チャネルに空きができる
sent 3             // ← チャネルに空きができたので、送信。そして、また一杯になる
received value: 1  // ← 1つ受信した(取り出した)ので、チャネルに空きができる
sent 4             // ← チャネルに空きができたので、送信
received value: 2
received value: 3
received value: 4

message passing

https://qiita.com/awakia/items/f8afa070c96d1c9a04c9 https://stackoverflow.com/questions/1853284/whats-the-difference-between-the-message-passing-and-shared-memory-concurrency

Releases

No releases published

Packages

No packages published