【Golang】構造体④コンストラクタ
Golangの基礎学習〜Webアプリケーション作成までの学習を終えたので、復習を兼ねてまとめていく。 基礎〜応用まで。
//コンストラクタ
//__init__の部分を書く
//Pythonの場合 クラス、メソッド
/*
class Vertex(object):
def __init__(self, x, y):
self._x = x
self._y = y
def area(self):
return self._x * self._y
def scale(self, i):
self._x = self._x * i
self._y = self._y * i
v = Vertex(3, 4)
v.scale(10)
print(v.area())
*/
package main
import (
"fmt"
)
type Vertex struct {
X, Y int
}
//2
//コンンストラクタのカスタム
//Pythonの def __init__(self):のイメージ
//NewStruct()のようにする。
func NewV(x, y int) *Vertex {
return &Vertex{x, y}
}
func main() {
//1
v := Vertex{3, 4}
fmt.Println(v)
v2 := NewV(3, 4)
//メモリー
fmt.Println(v2)
//実態
fmt.Println(*v2)
}