go结构体的定义

简介: go结构体的定义

一、结构体的定义

//结构定义
type Employee struct {
  Id   string
  Name string
  Age  int
}

二、实例的创建

 
 //初始化结构体
func TestCreateEmployeeObj(t *testing.T) {
  e := Employee{"0", "bob", 20}
  e1 := Employee{Name: "MIke", Age: 30}
  //返回指针
  e2 := new(Employee)
  e3 := Employee{}
  e.Name = "ccc"
  e2.Id = "2"
  e2.Age = 22
  t.Log(e)
  t.Log(e1)
  t.Log(e1.Id)
  t.Log(e2)
  t.Log(e3)
  t.Logf("e is %T", e)
  t.Logf("e2 is %T", e2)
}

三、结构体行为定义

//定义结构体的行为,实例访问
func (e Employee) String() string {
  fmt.Printf("String-Address is %x\n", unsafe.Pointer(&e.Name))
  return fmt.Sprintf("ID:%s-Name%s-age:%d", e.Id, e.Name, e.Age)
}
 
//定义结构体的行为,指针访问
func (e *Employee) StringTo() string {
  fmt.Printf("StringTo-Address is %x\n", unsafe.Pointer(&e.Name))
  return fmt.Sprintf("ID:%s-Name%s-age:%d", e.Id, e.Name, e.Age)
}
func TestStructOPerations(t *testing.T) {
  e := Employee{"0", "bob", 20}
  fmt.Printf("Address is %x\n", unsafe.Pointer(&e.Name))
  t.Log(e.String())
  t.Log(e.StringTo())
}

四、完整代码

package encapsulation
 
import (
  "fmt"
  "testing"
  "unsafe"
)
 
//结构定义
type Employee struct {
  Id   string
  Name string
  Age  int
}
 
//初始化结构体
func TestCreateEmployeeObj(t *testing.T) {
  e := Employee{"0", "bob", 20}
  e1 := Employee{Name: "MIke", Age: 30}
  //返回指针
  e2 := new(Employee)
  e3 := Employee{}
  e.Name = "ccc"
  e2.Id = "2"
  e2.Age = 22
  t.Log(e)
  t.Log(e1)
  t.Log(e1.Id)
  t.Log(e2)
  t.Log(e3)
  t.Logf("e is %T", e)
  t.Logf("e2 is %T", e2)
}
 
//定义结构体的行为,实例访问
func (e Employee) String() string {
  fmt.Printf("String-Address is %x\n", unsafe.Pointer(&e.Name))
  return fmt.Sprintf("ID:%s-Name%s-age:%d", e.Id, e.Name, e.Age)
}
 
//定义结构体的行为,指针访问
func (e *Employee) StringTo() string {
  fmt.Printf("StringTo-Address is %x\n", unsafe.Pointer(&e.Name))
  return fmt.Sprintf("ID:%s-Name%s-age:%d", e.Id, e.Name, e.Age)
}
func TestStructOPerations(t *testing.T) {
  e := Employee{"0", "bob", 20}
  fmt.Printf("Address is %x\n", unsafe.Pointer(&e.Name))
  t.Log(e.String())
  t.Log(e.StringTo())
}
=== RUN   TestCreateEmployeeObj
String-Address is c00007c880
    encap_test.go:26: ID:0-Nameccc-age:20
String-Address is c00007c940
    encap_test.go:27: ID:-NameMIke-age:30
    encap_test.go:28: 
String-Address is c00007c970
    encap_test.go:29: ID:2-Name-age:22
String-Address is c00007c9d0
    encap_test.go:30: ID:-Name-age:0
    encap_test.go:31: e is encapsulation.Employee
    encap_test.go:32: e2 is *encapsulation.Employee
--- PASS: TestCreateEmployeeObj (0.00s)
=== RUN   TestStructOPerations
Address is c00007ca90
String-Address is c00007cac0
    encap_test.go:49: ID:0-Namebob-age:20
StringTo-Address is c00007ca90
    encap_test.go:50: ID:0-Namebob-age:20
--- PASS: TestStructOPerations (0.00s)
PASS
相关文章
|
1月前
|
JSON 运维 Go
Go 项目配置文件的定义和读取
Go 项目配置文件的定义和读取
|
19天前
|
存储 Shell Go
Go语言结构体和元组全面解析
Go语言结构体和元组全面解析
|
1天前
|
存储 Go
Go: struct 结构体类型和指针【学习笔记记录】
本文是Go语言中struct结构体类型和指针的学习笔记,包括结构体的定义、成员访问、使用匿名字段,以及指针变量的声明使用、指针数组定义使用和函数传参修改值的方法。
|
1天前
|
安全 Go C语言
Go常量的定义和使用const,const特性“隐式重复前一个表达式”,以及iota枚举常量的使用
这篇文章介绍了Go语言中使用`const`定义常量的方法,包括常量的特性“隐式重复前一个表达式”,以及如何使用`iota`实现枚举常量的功能。
|
1月前
|
Go API
Go - 统一定义 API 错误码
Go - 统一定义 API 错误码
45 9
|
28天前
|
存储 设计模式 安全
空结构体:Go 语言中的轻量级占位符
【8月更文挑战第31天】
27 0
|
28天前
|
编译器 Go 开发者
|
1月前
|
存储 Java Go
Go从入门到放弃之结构体(面向对象)
Go从入门到放弃之结构体(面向对象)
|
1月前
|
消息中间件 缓存 API
go-zero微服务实战系列(三、API定义和表结构设计)
go-zero微服务实战系列(三、API定义和表结构设计)
|
1月前
|
存储 Go
Go 内存分配:结构体中的优化技巧
Go 内存分配:结构体中的优化技巧