Go では 複雑でコストのかかる処理を構文で隠してはいけないという基本的なルールがあります。

Go では複雑でコストのかかる処理を隠すような機能は実装されない []string を []interface{} に自動変換する良い方法や提供されている機能は無い func foo([]interface{}) { /* do somthing */ } func main() { var a[]string = []string{"hello", "world"} for(a) } 毎回以下のように実装する必要がある b = make([]interface{}, len(a), len(a)) for i := range a { b[i] = a[i] } 参考 go - Type converting slices of interfaces - Stack Overflow In Go, there is a general rule that syntax should not hide complex/costly operations. Converting a string to an interface{} is done in O(1) time....

2022-07-15 ·  2022-07-15 · 2 分 · 266 文字

Golang 組み込み構造体へのキャスト方法(Goのポリモーフィズムの実現)

Golang 組み込み構造体へのキャスト方法(Goのポリモーフィズムの実現) Golangで組み込み構造体(親)へのキャストを行いたい場合は(ポリモーフィズムを得るためには)、インターフェイスの実装が必要 子から親への参照はある インターフェースの名前はGolangでは er が慣例 package main import "fmt" type Parent struct { Attr1 string } type Parenter interface { GetParent() Parent } type Child struct { Parent //embed Attr string } func (c Child) GetParent() Parent { return c.Parent } func setf(p Parenter) { fmt.Println(p) } func main() { var ch Child ch.Attr = "1" ch.Attr1 = "2" setf(ch) } // result {{2} 1} 参考 go - Golang interface cast to embedded struct - Stack Overflow あなたは、継承を使ったオブジェクト指向のデザインパターンを使おうとしています。これはGoでのやり方ではありません。また、Javaや同等のオブジェクト指向言語では、インターフェース名は’able’で終わります。Goでは、インターフェイス名は’er’で終わるのが慣例です。 You are trying to use an object oriented design pattern with inheritance....

2022-07-11 ·  2022-07-11 · 1 分 · 210 文字

go generate assests box

go generate を使った assets ディレクトリの静的組み込みバイナリ処理 モノリポで管理しているSPAなクライアント側の成果物 (dist) を gin で静的配布 できないかと思った際に調べた内容。 結局使用していないが、バイナリ化する手順としては使えそうなのでメモしておく。 ディレクトリは以下の様な形 ├── Makefile ├── assets -> ../../client/back/dist ├── box │ ├── blob.go │ └── box.go └─generator.go Makefile まずは関係ないけど、Makefileって@書けば && バックスラッシュ連結とかいらんのかという発見 ./... 指定で全てを対象に指定 generate: @go generate ./... @echo "[OK] Files added to embed box!" generator.go 1行目はfmtをかけると自動付与された // +build ignore コメントにより通常ビルド対象から外れる設定 生成される go ファイルは blob.go で box 配下に出力される f, err := os.Create(blobFileName) や、 embedFolder string = "../assets/" から current directory は box 配下の模様 //go:build ignore // +build ignore package main import ( "bytes" "fmt" "go/format" "io/ioutil" "log" "os" "path/filepath" "strings" "text/template" ) const ( blobFileName string = "blob....

2022-06-22 ·  2023-04-26 · 3 分 · 559 文字

aws vault invalid client token id error

aws-vault で InvalidClientTokenId: The security token included in the request is invalid で怒られる │ Error: error reading IAM Role (rds-enhanced-monitoring-dev-db): InvalidClientTokenId: The security token included in the request is invalid │ status code: 403, request id: d09fe380-0c00-4485-9aeb-1e58740fdcb3 ここに答えがあった Error: The security token included in the request is invalid · Issue #260 · 99designs/aws-vault · GitHub I’ve encountered the same problem. It’s caused by the AWS API restrictions, that won’t allow you to touch IAM related APIs using the account with MFA enabled, unless you pass the MFA token within the request....

2021-12-02 ·  2023-09-22 · 1 分 · 125 文字

別ファイルに書いた main パッケージの Function が undefined 判定される

別ファイルに書いた main パッケージの Function が undefined 判定される ここに答えがあった go - “undefined” function declared in another file? - Stack Overflow Please read “How to Write Go Code”. Use go build or go install within the package directory, or supply an import path for the package. Do not use file arguments for build or install. While you can use file arguments for go run, you should build a package instead, usually with go run ....

2021-11-26 ·  2023-09-22 · 1 分 · 97 文字