zoukankan      html  css  js  c++  java
  • go学习笔记day13

    Go 语言结构体

    Go 语言中数组可以存储同一类型的数据

    结构体中我们可以为不同项定义不同的数据类型

    结构体是由一系列具有相同类型或不同类型的数据构成的数据集合

    结构体表示一项记录,比如保存图书馆的书籍记录,每本书有以下属性:
    
    Title :标题
    Author : 作者
    Subject:学科
    ID:书籍ID
    

    定义结构体

    结构体定义需要使用 type 和 struct 语句。

    struct 语句定义一个新的数据类型,结构体中有一个或多个成员。

    type 语句设定了结构体的名称。结构体的格式如下:

    // 一旦定义了结构体类型,它就能用于变量的声明
    type struct_variable_type struct {
       member definition
       member definition
       ...
       member definition
    }

    示例

    package main
    
    import "fmt"
    
    type Books struct {
       title string
       author string
       subject string
       book_id int
    }
    
    
    func main() {
    
        // 创建一个新的结构体
        fmt.Println(Books{"Go 语言", "www.runoob.com", "Go 语言教程", 6495407})
    
        // 也可以使用 key => value 格式
        fmt.Println(Books{title: "Go 语言", author: "www.runoob.com", subject: "Go 语言教程", book_id: 6495407})
    
        // 忽略的字段为 0 或 空
       fmt.Println(Books{title: "Go 语言", author: "www.runoob.com"})
    }
    

    结构体作为函数参数

    结构体指针

    var struct_pointer *Books
    struct_pointer = &Book1 // 可以存储结构体变量的地址

    // 使用结构体指针访问结构体成员,使用 "." 操作符:
    struct_pointer.title
     

      

  • 相关阅读:
    mysql的存储过程
    一份php高级工程师的面试题,我每天看一点点
    php的常用函数(持续更新)
    php 中文字符串截取
    php递归遍历文件目录
    ajax timeout 断网处理
    angular 刷新问题
    angular state中templateUrl 路径的模板
    angular请求传递不了数据
    截取字符串 substring substr slice
  • 原文地址:https://www.cnblogs.com/baota/p/15695045.html
Copyright © 2011-2022 走看看