2
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?

More than 5 years have passed since last update.

Go言語のnewという組み込み関数

Posted at

まえがき

Go言語によるWebアプリケーション開発を読んでいると、以下のコードがでてきた。

new(client)

前提としてclientは自分で定義した構造体型。

自分では実装していない関数だ。
本の中で説明されていたのだろうか。
覚えていない。

出てきたのかもしれないし、自明だということで説明が省かれているのかもしれない。

ということで調べた。

func new とは

func new(Type) *Type

シグネチャを見ると、引数に型を取り、型のポインタを返す関数のようだ。
説明を読むと、受け取った型をゼロ値で初期化したときのポインタを返す組み込み関数のようだった。

コードの意味

つまり、以下のコードはclient型をゼロ値で初期化して、そのポインタを返すという意味だった。

new(client)

おわり

ps
シグネチャの使い方あってますか??

2
0
2

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
2
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?