🧑‍🏫
liualexiang
  • Introduction
  • Azure
    • AKS Basic
    • AKS Spark
    • AZ ACR SYNC
    • Azure CMI SDWAN
    • Azure LB DeepDive
      • Azure LB DeepDive
    • Azure Service Principal Basic
    • Azure Internal VM Network Connectivity
      • Azure Internal VM Network Connectivity
    • Azure Cli Build
    • Azure Vm Memory Monitor
  • Blockchain
    • BTC
  • CRISPR
    • 使用Parallel_Cluster提升CRISPA效率
  • OpenSource
    • ElasticSearch
      • ES Get Started
      • ES Search Query
      • Kibana 可视化
      • Logstash配置
    • Ansible 基础
    • Infra As Code
      • Pulumi Get Started
      • Terraform Basic
    • ZooKeeper 基础
    • RPC与REST
    • 使用Python申请大量内存测试
    • 使用TPC_DS产生压测数据
    • Superset
      • Superset部署手册
    • 代码扫描
    • Git
      • Git Basic
      • Github Action Basic
      • Gitlab与AzureAD集成
      • Gitbook 基础教程
    • K8S
      • enter_node
      • K8s X509 Client Cert
      • K8s Basic
      • K8s Oidc
      • Docker 基础
      • helm基础
      • K8S_Secrets管理
      • 深入了解K8S
      • 混沌工程
      • Istio
      • 生态
      • CRD开发
      • k8s网络
    • Cloud_Custodian
    • Jenkins Basic
    • Nginx
    • ETCD
    • 正则
    • VictoriaMetrics
    • Kafka
  • MySQL
    • MySQL 调优
  • Linux
    • SSH Tunnel 上网
    • 内存管理
    • 在Linux系统中通过LUKS加密磁盘
    • 量子计算 Basic
    • IO多路复用
    • Iptables
    • tmux和screen
    • Systemd
    • OS 基础
    • jq基础
    • yum
    • neovim
  • Web
    • Html Css
    • Web部署
    • 缓存
  • Programming
    • 算法
      • 返回list中最大生序子序列长度
    • Python技巧
      • Python的语法糖
      • Python常用装饰器
      • AsyncIO基础
      • 自动化测试pytest
      • python中的下划线
      • 面向对象
      • Python的坑
      • Python配置文件管理
      • HTTP Stream Response
      • Python项目管理
    • 设计模式
      • 设计模式
      • 面向对象的思想
      • 编程概念
    • Go
      • Go 基础
      • Go常用功能
      • 结构体入门
    • 前端
    • Vue
    • NodeJS
  • Math
    • 多项式插值法
  • Security
    • HTTP常见攻击
    • 加密与签名
    • RSA
    • ECDSA
  • Solidity
    • Solidity基础
    • Blockchain Testnet Faucet
  • Tools
    • 视频处理ffmpeg
    • IDE配置
    • iTerm2美化
    • 密码管理
    • FRP配置
    • 工具集
由 GitBook 提供支持
在本页
  • hello world
  • 值接收器和指针接收器
  1. Programming
  2. Go

结构体入门

hello world

结构体创建

向其中赋值的示例

type Book struct {  
    Title      string  
    Author     string  
    Year       int  
    CheckedOut bool  
}  
  
func main() {  
    b1 := Book{}  
    info := []byte(`{"Title":"a","Author":"b", "Year": 1, "CheckedOut": false}`)  
    _ = json.Unmarshal(info, &b1)  
  
    fmt.Printf("book title is %s", b1.Title)  
}

go 的json unmarshal 是可以只将部署值传过去的,示例


const myData = `{
		"name":"alex",
		"age":3012312123423423421
	}`

type myObj struct {
	Age int64 `json:"age"`
}

func main() {
	//obj := make(map[string]interface{})
	obj := myObj{}
	_ = json.Unmarshal([]byte(myData), &obj)
	data, _ := json.Marshal(obj)
	fmt.Println(string(data))
}

结构体方法

通过字符串返回

func (b *Book) printName() string {  
    returnMsg := "print name book title: " + b.Title  
    return returnMsg  
}  
  
func main() {  
    b1 := Book{}  
    info := []byte(`{"Title":"a","Author":"b", "Year": 1, "CheckedOut": false}`)  
    _ = json.Unmarshal(info, &b1)  
  
    getBookTitle := b1.printName()  
  
    fmt.Println(getBookTitle)  
}

通过地址返回

type Book struct {  
    Title      string  
    Author     string  
    Year       int  
    CheckedOut bool  
}  
  
func (b *Book) printName() *string {  
    returnMsg := "print name book title: " + b.Title  
    return &returnMsg  
}  
  
func main() {  
    b1 := Book{}  
    info := []byte(`{"Title":"a","Author":"b", "Year": 1, "CheckedOut": false}`)  
    _ = json.Unmarshal(info, &b1)  
  
    getBookTitle := *b1.printName()  
  
    fmt.Println(getBookTitle)  
}

通过地址返回也可以这么写

b1 := Book{}  
info := []byte(`{"Title":"a","Author":"b", "Year": 1, "CheckedOut": false}`)  
_ = json.Unmarshal(info, &b1)  
  
getBookTitle := b1.printName()  
  
fmt.Println(*getBookTitle)

值接收器和指针接收器

无论是值接收器,还是指针接收器,都可以给 struct 添加方法,只是值接收器里的函数,是将结构体实例化的对象拷贝了一份,对副本操作。而指针接收器,则是直接操作结构体实例化对象,能直接修改这个对象本身的属性。一个示例如下

  
type Rectangle struct {  
    Width  float64  
    Height float64  
}  
  
func (r Rectangle) Area() float64 {  
    return r.Width * r.Height  
}  
  
func (r *Rectangle) Scale(factor float64) {  
    r.Width *= factor  
    r.Height *= factor  
}  
  
func main() {  
    rec := &Rectangle{  
       Width:  4,  
       Height: 5,  
    }  
    originalArea := rec.Area()  
    fmt.Printf("original area was %f\n", originalArea)  
  
    rec.Scale(2)  
    newArea := rec.Area()  
    fmt.Printf("new area was %f", newArea)  
}

上面是一个基本的例子,能看出来,刚开始初始化了一个 宽4高5的长方形,之后直接用值接收器的方法,获得面积。之所以这里用值传递,是因为我们没有改变这个长方形实例的任何属性,都是拿长方形实例的属性做额外的计算。之后我们在Scale方法里,修改了长方形的宽和高,由于是对长方形实例本身进行操作,所以要用指针接收器。

再来一个更复杂一点的例子: 在下面的例子里,我们定义了 Book 和 Library,其中Library 的 Books是一个切片,Library结构体有AddBook和CheckOutBook方法,当AddBook的时候,会将书添加到Books切片里,当CheckOutBook的时候,会从Books 切片里移除这本书。Book结构体本身只有一个DisplayInfo的方法,将书的信息打印出来。


type Book struct {  
    Title      string  
    Author     string  
    Year       int  
    CheckedOut bool  
}  
  
type Library struct {  
    Books []Book  
}  
  
func (b Book) DisplayInfo() {  
    fmt.Printf("Title: %s, Author: %s, Year: %d, CheckedOut: %t\n", b.Title, b.Author, b.Year, b.CheckedOut)  
}  
  
func (l *Library) AddBook(b Book) {  
    l.Books = append(l.Books, b)  
}  
  
func (l *Library) CheckOutBook(BookName string) {  
    for i := range l.Books {  
       if l.Books[i].Title == BookName && !l.Books[i].CheckedOut {  
          l.Books[i].CheckedOut = true  
          fmt.Printf("successfully checked out: %s \n", l.Books[i].Title)  
          return  
       }  
    }    fmt.Printf("Book not found or already checked out %s \n", BookName)  
}  
  
func main() {  
    myLibrary := &Library{}  
    b1 := Book{  
       Title:      "The Go Programming Language",  
       Author:     "Alan A. A. Donovan",  
       Year:       2015,  
       CheckedOut: false,  
    }  
    b2 := Book{  
       Title:      "The Pragmatic Programmer",  
       Author:     "Andrew Hunt",  
       Year:       1999,  
       CheckedOut: false,  
    }  
    myLibrary.AddBook(b1)  
    myLibrary.AddBook(b2)  
  
    borrow := "The Go Programming Language2"  
    fmt.Printf("checking out %s\n", borrow)  
    myLibrary.CheckOutBook(borrow)  
}
上一页Go常用功能下一页前端

最后更新于6个月前