Skip to content

Instantly share code, notes, and snippets.

@takashimamorino
Last active July 30, 2023 07:07
Show Gist options
  • Save takashimamorino/07523e6741d79686c2b21ec416cc0302 to your computer and use it in GitHub Desktop.
Save takashimamorino/07523e6741d79686c2b21ec416cc0302 to your computer and use it in GitHub Desktop.
Golang

Golang

構造体を定義して、関数の引数に渡す

golang には名前付き引数がない

func main() {
	MyFunc(MyFuncOpts{
		X: "x",
		Y: "y",
	})
}

type MyFuncOpts struct {
	X string
	Y string
}

func MyFunc(opts MyFuncOpts) {
	return
}

メソッド

  • 型に付随する関数
  • 関数と違いレシーバが追加される
    • 例で言う 『(p Person)』
    • レシーバを書くことで型と結びつけられる
func main() {
	p := Person{"John", 42}
	p.Greet()
}

type Person struct {
	Name string
	Age  int
}

func (p Person) Greet() {
	println("Hello", p.Name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment