Go 语言结构
来源:dev.to
时间:2024-08-30 23:06:48 435浏览 收藏
目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《Go 语言结构》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~
在 golang 中,结构是数据的简单容器。
- 可以有字段
- 没有附加任何方法
- 单独定义并与结构类型关联的方法。
下面显示了 ruby 和 golang 中的简单 book 类等效项。
class book attr_reader(:title, :author) def initalize(title, author) @title = title @author = authoer end end # usage book = book.new('title', 'jon snow')
// equivalent to `class book` in ruby type book struct { title string, author string }
实例化 golang 类型
复合文字
复合文字是一步创建初始化复合类型的语法。我们可以实例化以下类型:
- 结构
- 数组
- 切片
- 地图
这里我们将一个新的 book 实例分配给变量 book
// composite literal book := book{ title: "title", author: "author" }
使用新关键字
较长的形式是使用 new 关键字。这类似于我们在 ruby 中使用 book = book.new(..)
实例化一个类的方式我们将使用 = 符号分配书籍的属性(即标题和作者)。
// using the `new` keyword book := new(book) book.title = "book title" book.author = "john snow"
没有短变量声明 (:=)
注意到我们在第一个示例中使用了符号 := 吗?
它是以下声明变量并为其赋值的详细方式的语法糖。
// without short virable declaration // example 1 var book book // declare variable `book` of type `book` book.title = "book title" // assign the value to book variable book.author = "john snow" // example 2 var count int count = 20
工厂功能
当我们需要时,我们还可以使用工厂模式来简化结构体的初始化:
- 添加额外逻辑
- 添加默认值
假设我们希望将书名和作者标记的每个第一个字符大写。
// factory function func newbook(title string, author string) book { return book{ title: titlelise(title), // default logic to "titlelise" author: titlelist(author) } } func titlelise(str string) { caser := cases.title(lanaguage.english) return caser.string(str) }
将函数附加到结构体
在 ruby 中,我们只需在类中定义一个函数即可。在这里,我们定义一个名为 to_string() 的函数来打印书名作者。
class book attr_reader(:title, :author) def initalize(title, author) @title = title @author = authoer end # new function we added def to_string() put "#{title} by #{string}" end end
在 golang 中,我们通过将结构传递给函数来“附加”函数。
// equivalent to `class book` in ruby type book struct { title string, author string } // attaching the function to the `struct` func (book book) tostring() string { return fmt.sprintf("%s by %s", book.title, book.author) } // usage book := book{ title: "title", author: "author" } book.tostring() // => title by author
解释:
func (book Book) ToString() string
token | description |
---|---|
func | function keyword |
(book book) | attaching the function to the type book struct - book: variable to access the struct within the function - book: type of the struct |
tostring() | name of the function |
string | return type of the function |
今天关于《Go 语言结构》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
505 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
197 收藏
-
338 收藏
-
370 收藏
-
380 收藏
-
338 收藏
-
370 收藏
-
268 收藏
-
103 收藏
-
424 收藏
-
186 收藏
-
380 收藏
-
403 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习