0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Go]パート4

Posted at

はじめに

訳あってGo言語を勉強することになりました。
完全に自分のメモ用です。
馴染みのない書き方を中心にメモしていきます。

func

main.go
package main

import "fmt"

func main() {
	var a int
	var b int

	a, b = test(5, 7)

	fmt.Println(a)
	fmt.Println(b)
}

func test(x int, y int) (int, int) {
	return x * y, x + y
}

結果

35
12

戻り値を複数返すことが可能です。

ポインタ

package main

import "fmt"

func main() {
	var val1 int

	fmt.Println(val1)
	change(&val1)
	fmt.Println(val1)

}

func change(hoge *int) {
	fmt.Println(hoge)
	*hoge = 4
}

結果

0
0xc00009c018
4

参照渡しはポインタを使用します。

構造体

main.go
package main

import "fmt"

type Person struct {
	name string
	age  int
}

func (p *Person) SetPerson(name string, age int) {
	p.name = name
	p.age = age
}

func (p *Person) GetPerson() (string, int) {
	return p.name, p.age
}

func main() {
	var person Person
	person.SetPerson("太郎", 23)

	name, age := person.GetPerson()

	fmt.Println(name)
	fmt.Println(age)
}

結果

太郎
23

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?