0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Golang】構造体④コンストラクタ

Posted at

【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)
}

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?