basebank / gophers-code-reading-party

Gophers Code Reading Party records
18 stars 1 forks source link

20210812 Gophers Code Reading Party #9

Closed budougumi0617 closed 1 year ago

budougumi0617 commented 3 years ago

8/9の週に開催予定です。 8/12(木) 14:00〜15:00@ Zoom, BASE BANK, Inc.

日程調整さん https://chouseisan.com/s?h=68bee7000a0e43ecb1a6c548ab9c425b

参加者

お題

budougumi0617 commented 3 years ago

8/9の週に開催予定です。 日程調整さん https://chouseisan.com/s?h=68bee7000a0e43ecb1a6c548ab9c425b

budougumi0617 commented 3 years ago

BASEメンバーの予定はざっくりみて日程登録済みです。

budougumi0617 commented 2 years ago

エウレカのみなさんは休暇のようでしたのでBASE的に一番参加率高そうな8/12(木) 14:00〜15:00にしておきます!!!

budougumi0617 commented 2 years ago

https://zoom.us/j/97853855980?pwd=SElGRXhIQ3hZc2YvMWI1cVlia2lidz09

budougumi0617 commented 2 years ago

TwitterにzoomのURLを流すと乱入されてしまうらしい(余談)

budougumi0617 commented 2 years ago

BASEメンバーオンリーなので社内共通ライブラリの解説中。

hgsgtk commented 2 years ago

補足ありがとうございます!

hgsgtk commented 2 years ago

Go 1.17 (次回のネタの先行集めをしてみる)

https://tip.golang.org/doc/go1.17

Pruned module graphs in go 1.17 modules

To facilitate the upgrade to Go 1.17 pruned module graphs, the go mod tidy subcommand now supports a -go flag to set or change the go version in the go.mod file. To convert the go.mod file for an existing module to Go 1.17 without changing the selected versions of its dependencies, run:

humuhumu

https://tip.golang.org/ref/mod#graph-pruning Module graph pruning

Minor changesめっちゃあるな

以降 @hgsgtk 的に気になった内部実装をぽちぽちいれていく

hgsgtk commented 2 years ago

File.WriteStringの最適化

https://tip.golang.org/doc/go1.17#os

The File.WriteString method has been optimized to not make a copy of the input string.

ユーザーとしても参考になりそう

hgsgtk commented 2 years ago

strconvのアルゴリズム

po3rinさんが好きそうな話題が見受けられる

The strconv package now uses Ulf Adams's Ryū algorithm for formatting floating-point numbers. This algorithm improves performance on most inputs and is more than 99% faster on worst-case inputs.

Ulf Adams's Ryū algorithm なにこれたのしそう

https://tip.golang.org/doc/go1.17#strconv

hgsgtk commented 2 years ago

testing.T.SetEnv がきたぜ

The new T.Setenv and B.Setenv methods support setting an environment variable for the duration of the test or benchmark.

うれしいねぇ

https://tip.golang.org/doc/go1.17#testing

hgsgtk commented 2 years ago

time packageの細かすぎて気になった

https://tip.golang.org/doc/go1.17#time

The package now accepts comma "," as a separator for fractional seconds when parsing and formatting time. The following time formats are now accepted:

2006-01-02 14:06:03,999999999 -0700 MST Mon Jan _2 14:06:03,120007 2006 Mon Jan 2 14:06:03,120007 2006

The new constant Layout defines the reference time.

commaオッケーにしたよとかそういう細かすぎて気づかんみたいなやつ好き、にこにこしちゃう

hgsgtk commented 2 years ago

flag packageのエラーハンドリング

https://tip.golang.org/doc/go1.17#flag

Flag declarations now panic if an invalid name is specified.

コードをサクッと読んでふ〜んってする分にはたのしそう

budougumi0617 commented 2 years ago

futureさんの連載見ればキャッチアップできるはず… https://future-architect.github.io/articles/20210810a/

hgsgtk commented 2 years ago

https://qiita.com/eihigh/items/9fe52804610a8c4b7e41 なんとなく目に入ったgo get

hgsgtk commented 2 years ago

https://gocon.connpass.com/event/216361/?utm_campaign=event_publish_to_follower&utm_source=notifications&utm_medium=twitter

金曜日

hgsgtk commented 2 years ago
$ go get golang.org/dl/go1.17beta1
$ go1.17beta1 download
$ go1.17beta1 version
go version go1.17beta1 darwin/amd64

ちゃっとちゃっとした

budougumi0617 commented 2 years ago

ライブコーディングは練習していないと失敗する()

hgsgtk commented 2 years ago

flag 該当のやつ

https://go-review.googlesource.com/c/go/+/271788/

image

テストコードで試してみる

package main

import (
    "bytes"
    "flag"
    "fmt"
    "testing"
)

type flagVar []string

func (f *flagVar) String() string {
    return fmt.Sprint([]string(*f))
}

func (f *flagVar) Set(value string) error {
    *f = append(*f, value)
    return nil
}

func TestInvalidFlags(t *testing.T) {
    tests := []struct {
        flag     string
        errorMsg string
    }{
        {
            flag:     "-foo",
            errorMsg: "flag \"-foo\" begins with -",
        },
        {
            flag:     "foo=bar",
            errorMsg: "flag \"foo=bar\" contains =",
        },
    }

    for _, test := range tests {
        testName := fmt.Sprintf("FlagSet.Var(&v, %q, \"\")", test.flag)

        fs := flag.NewFlagSet("", flag.ContinueOnError)
        buf := bytes.NewBuffer(nil)
        fs.SetOutput(buf)

        var v flagVar
        fs.Var(&v, test.flag, "")
        if msg := test.errorMsg + "\n"; msg != buf.String() {
            t.Errorf("%s\n: unexpected output: expected %q, bug got %q", testName, msg, buf)
        }
    }
}

これはflat_test.goからもってきた

Go1.1より前の時代

 go test main_test.go 
--- FAIL: TestInvalidFlags (0.00s)
    main_test.go:46: FlagSet.Var(&v, "-foo", "")
        : unexpected output: expected "flag \"-foo\" begins with -\n", bug got ""
    main_test.go:46: FlagSet.Var(&v, "foo=bar", "")
        : unexpected output: expected "flag \"foo=bar\" contains =\n", bug got ""
FAIL
FAIL    command-line-arguments  0.096s
FAIL

Go1.17 (beta1つかってる)

$ go1.17beta1 test main_test.go
--- FAIL: TestInvalidFlags (0.00s)
panic: flag "-foo" begins with - [recovered]
        panic: flag "-foo" begins with -

goroutine 18 [running]:

(omit)
FAIL    command-line-arguments  0.207s
FAIL
budougumi0617 commented 2 years ago

完全に理解した。flag.ParseというかGoのコマンドラインオプションは引数のあとにフラグをつけられないんだった。

budougumi0617 commented 2 years ago

func main() {
        fs := flag.NewFlagSet("ExampleValue", flag.ExitOnError)
        fs.Var(&URLValue{u}, "url", "URL to parse")

        fmt.Println(os.Args[1:])
        fs.Parse(os.Args[1:])
        fmt.Printf(`{scheme: %q, host: %q, path: %q}`, u.Scheme, u.Host, u.Path)
}

fs.Var(&URLValue{u}, "url", "URL to parse") みたいにflagをせっていする(この場合-urlになる)わけですが、ここで、-urlとかurl=urlみたいなフラグを宣言するとpanicするようにするだったんですね。

hgsgtk commented 2 years ago

Currently, the flag package allows any string to be used as the name of the flag. It only validates whether the name of the flag is already registered. (code)

But, if the name of the flag is (1) empty, (2) starts with a hyphen, or (3) contains an equal sign, the flag can not be parsed properly. If you try to use the flag as described in the usage message, it will fail. (code)

https://github.com/golang/go/issues/41792

ParseでうまくいかんのやからValidateちゃんとしようよぉみたいなのがproposalだった

的なことをさっくりとpublishした https://zenn.dev/hgsgtk/articles/9f662a4c96fa3f

budougumi0617 commented 2 years ago
package main

import (
        "flag"
        "fmt"
        "net/url"
        "os"
)

type URLValue struct {
        URL *url.URL
}

func (v URLValue) String() string {
        if v.URL != nil {
                return v.URL.String()
        }
        return ""
}

func (v URLValue) Set(s string) error {
        if u, err := url.Parse(s); err != nil {
                return err
        } else {
                *v.URL = *u
        }
        return nil
}

var u = &url.URL{}

func main() {
        fs := flag.NewFlagSet("ExampleValue", flag.ExitOnError)
        fs.Var(&URLValue{u}, "-url", "URL to parse") // ------------------------- "-url" にしてある

        fmt.Println(os.Args[1:])
        fs.Parse(os.Args[1:])
        fmt.Printf(`{scheme: %q, host: %q, path: %q}`, u.Scheme, u.Host, u.Path)

}
$ go version
go version go1.16.6 darwin/amd64

$ go run ./main.go
[]
{scheme: "", host: "", path: ""}%
$ go1.17rc2 run main.go
flag "-url" begins with -
panic: flag "-url" begins with -

goroutine 1 [running]:
flag.(*FlagSet).Var(0xc0000581e0, {0x10d2a00, 0xc00000e028}, {0x10b1e4c, 0x11bc108}, {0x10b2f6c, 0x1158240})
        /Users/yoichiroshimizu/sdk/go1.17rc2/src/flag/flag.go:864 +0x439
main.main()
        /Users/yoichiroshimizu/work/flag/main.go:34 +0xf2
exit status 2
hgsgtk commented 2 years ago

Currently, the flag package allows any string to be used as the name of the flag. It only validates whether the name of the flag is already registered. (code)

But, if the name of the flag is (1) empty, (2) starts with a hyphen, or (3) contains an equal sign, the flag can not be parsed properly. If you try to use the flag as described in the usage message, it will fail. (code)

https://github.com/golang/go/issues/41792

ParseでうまくいかんのやからValidateちゃんとしようよぉみたいなのがproposalだった

hgsgtk commented 2 years ago

Parseがうまくいかんっつうのはどういうことかっていうと

    _ = fs.Parse([]string{"--hyphen"}) // bad flag syntax: -=foobar

ってやったとてflag provided but not defined: -hyphenっていうエラーになるし、

    _ = fs.Parse([]string{"-=foobar"}) // bad flag syntax: -=foobar

ってやったとしてbad flag syntax: -=foobarってなるわけでParse時に期待したものが出てこない。CLI作成者とかが期待したflag設定がこの時点で来ていないのでどこかでValidateしてあげなあかんのちゃう?ってのがProposalの課題意識