go:embed使ってツールをリリースするまで
TIME rest time current/total
TopicsPlaceHolder

go:embed使ってツールをリリースするまで

TechFeed Summit#5

Mar 18th, 2021

Profile

songmu

【宣伝】Nature Remo

【宣伝】Natureスマート電気

本題

godzil

https://github.com/Songmu/godzil

DEMO

godzil new

profile(ひな形)の例

package main

import (
    "context"
    "flag"
    "log"
    "os"

    "{{.PackagePath}}"
)

func main() {
    log.SetFlags(0)
    err := {{.Package}}.Run(context.Background(), os.Args[1:], os.Stdout, os.Stderr)
    if err != nil && err != flag.ErrHelp {
        log.Println(err)
        exitCode := 1
        if ecoder, ok := err.(interface{ ExitCode() int }); ok {
            exitCode = ecoder.ExitCode()
        }
        os.Exit(exitCode)
    }
}

profile(ひな形)の同梱

rakyll/statikからgo:embedに乗り換え

https://github.com/rakyll/statik

ハマったところ

go.mod問題

以下のようなエラーが発生。

pattern testdata/assets/*: cannot embed directory testdata/assets/basic: in different module

アセット内にgo.modがあるとエラーになる。たしかにこのあたりは仕様的に慎重になったほうが良さそう。

if _, err := fsys.Stat(filepath.Join(dir, "go.mod")); err == nil {
    return nil, nil, fmt.Errorf("cannot embed %s %s: in different module", what, rel)
}

https://github.com/golang/go/blob/go1.16.2/src/cmd/go/internal/load/pkg.go#L2001-L2003

対応

dotfiles問題

If a pattern names a directory, all files in the subtree rooted at that directory are embedded (recursively), except that files with names beginning with ‘.’ or ‘_’ are excluded.
-- https://golang.org/pkg/embed/#hdr-Directives

直接指定でいけるという話もあるようだが、うまくいかなかった…。

対応

まとめ

宣伝

OSS活動を支援してくれると嬉しいです。

We are hiring!