总结
豆包 AI 助手文章总结
首页 > 后端开发 > Golang > 正文

Go 语言结构

WBOY
发布: 2024-08-30 09:09:34
转载
616人浏览过

go 语言结构

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
登录后复制
令牌 描述 标题> 函数 函数关键字 (书本) 将函数附加到 book 结构类型
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
- book:用于访问函数内结构的变量- book:结构的类型 tostring() 函数名称 字符串 函数的返回类型 表>
>

以上就是Go 语言结构的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号