Structure of the basics of Go language (summer chapter)

Release: 2023-07-24 17:38:26
forward
823 people have browsed it

Constructor function

Constructor function is the same as in other languages. The official understanding is The method of is executed when the class is instantiated, usually used for assignment operations.

But in Go, it may not be the same, and you need to use a separate function to complete it.

Structure

type Student struct {
    Name  string
    Age   int
    phone string
}
Copy after login

Constructor

func NewStudent(name string, age int, phone string) *Student {
    return &Student{Name: name, Age: age, phone: phone}
}
//函数尽量采用固定格式 New结构体名
Copy after login

Assignment operation

func main() {
    var s1 = NewStudent("张三", 18, "1111")
    fmt.Println(s1)
}
Copy after login

Execution results

Structure of the basics of Go language (summer chapter)

##Why does the constructor return a structure pointer

Generally speaking, there are two reasons. The first reason is that the performance of passing the address is higher. The second reason is because of the specification. The same is true for the subsequent function binding structure, and more. A norm.

不太用纠结说指针怎么怎么看不懂,对于结构体来说,是不是指针,其实用法都一样。


函数绑定结构体

如果你有其他语言的基础,你可能对于类和对象比较熟悉,传统做法中,是将方法写入类中的。

但是在Go中,采用绑定的方式添加方法。

语法

func (一般用this 要绑定的结构体) 函数名([参数1,参数2...]) [(返回值1,返回值2,...)]{
    代码
}
//一般用this,也可以用其他的,this就像形参一样,随便换,用self,用p,用s,都一样的
Copy after login

示例:给Student结构体绑定方法。

func (this Student) say() {
    fmt.Printf("我是%v,我今年%v岁了,我的手机号是%v\n", this.Name, this.Age, this.phone)
}
Copy after login

main代码

func main() {
    //调用构造方法
    var s1 = NewStudent("张三", 18, "1111")
    //调用Student绑定的say方法
    s1.say()
}
Copy after login

执行结果

Structure of the basics of Go language (summer chapter)

有没有感觉有点Java和Python的感觉了,上述可是通过结构体的方式调用方法的,这里就和C区分开了。

在Go中,基本就是通过这些操作,模拟出来面向对象的,相比之下,我更习惯Go的方式,更加灵活。


函数绑定结构体(指针方式)

如果说区别,只是将要修改的 要绑定的结构体 前面加一个*

代码

func (this *Student) say() {
    fmt.Printf("我是%v,我今年%v岁了,我的手机号是%v\n", this.Name, this.Age, this.phone)
}
Copy after login

执行结果和上述一摸一样。


函数绑定结构体(指针方式和普通方式区别)

通常来说,一般使用指针的方式居多。

嗯...不是居多,是基本都是。

区别

代码一

func (this Student) say1() {
    fmt.Printf("我是%v,我今年%v岁了,我的手机号是%v\n", this.Name, this.Age, this.phone)
    this.Name = "666"//这里修改了Name为其他值
}
Copy after login

第3行修改了Name

func main() {
  //调用构造方法
  var s1 = NewStudent("张三", 18, "1111")
  //调用Student绑定的say方法
  s1.say1()
  //打印s1.Name
  fmt.Println(s1.Name)
}
Copy after login
Copy after login

第7行又打印了s1.Name

执行结果

Structure of the basics of Go language (summer chapter)

???结果没修改,what。

代码二

func (this *Student) say1() {
  fmt.Printf("我是%v,我今年%v岁了,我的手机号是%v\n", this.Name, this.Age, this.phone)
  this.Name = "666"//这里修改了Name为其他值
}
Copy after login

第一行修改为*

func main() {
  //调用构造方法
  var s1 = NewStudent("张三", 18, "1111")
  //调用Student绑定的say方法
  s1.say1()
  //打印s1.Name
  fmt.Println(s1.Name)
}
Copy after login
Copy after login

执行结果

Structure of the basics of Go language (summer chapter)

这次可以看到,结果变了,在其他函数修改了Name,影响了整个s1的Name。


Conclusion

  • When using functions to bind structures, try to integrate the structure parameters into *Type of.

  • One is because of the specification, and the other is because object-oriented should be like this. Modifying the properties of an object should theoretically affect the entire object value.

The above is the detailed content of Structure of the basics of Go language (summer chapter). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Go语言进阶学习
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!