make()new()
init()
无论如何,他们之间有什么区别?
更新我回答了我自己的问题,请检查一下,看看我的答案.
1> Tarik..:
我想我已经想到并决定分享我到目前为止的想法.
make()new()
make()new()
newnewint0structnil""string
mapslicechannelListArrayListnewnilnullmake()slices
new()nilnil
new()
public class Person{ public string Name; public int Age; public Address HomeAddress; } var person = new Person(); Console.WriteLine(person.Name); // "" Console.WriteLine(person.Age); // 0 Console.WriteLine(person.HomeAddress); // null
make()slicemapchannel
public class PersonList{ // We are initializing the array so that we can use it. // Its capacity can increase. private Person[] _persOnList= new Person[100]; public void Add(Person p){} public void Remove(Person p){} public Person Get(int index){} }
初始化与归零
0nil""falsehello := Hello{name="world"}var hello = new Hello() {Name = "World"}Hellonameworld
new List()make)
(http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,cf7f4095e4de7646):
newmake()
2> OneOfOne..:
new
intPtr := new(int) // or var i int intPtr := &i
make
无论您如何创建变量,所有变量都会在创建时归零.
关于make的规范:https://golang.org/ref/spec#Making_slices_maps_and_channels
关于零值的规范:https://golang.org/ref/spec#The_zero_value