Encapsulating Data and Behavior
Structure definition
type Employee struct {
Id string
Name string
Age int
}
Instance creation and initialization
e := Employee{
"0", "Bob", 20}
e1 := Employee{
Name: "Mike", Age: 30}
e2 := new(Employee) //注意这里返回的引⽤用/指针,相当于 e := &Employee{}
e2.Id = “2" //与其他主要编程语言的差异:通过实例例的指针访问成员不需要使用->
e2.Age = 22
e2.Name = “Rose"
Behavior (method) definition
Difference Demo with other major programming languages
:
package objectStruct
import (
"fmt"
"testing"
"unsafe"
)
type Employee struct {
Id string
Name string
Age int
}
func (e *Employee) String() string {
fmt.Printf("Adress is %x", unsafe.Pointer(&e.Name))
return fmt.Sprintf("ID:%s/Name:%s/Age:%d", e.Id, e.Name, e.Age)
}
//func (e Employee) String() string {
// fmt.Printf("Address is %x\n", unsafe.Pointer(&e.Name))
// return fmt.Sprintf("ID:%s-Name:%s-Age:%d", e.Id, e.Name, e.Age)
//}
func TestCreateEmployeeObject(t *testing.T) {
e := Employee{
"11", "liSi", 26}
e1 := Employee{
Name: "wangWu", Age: 21}
e2:= new(Employee) // 返回指针
e2.Id = "3"
e2.Age = 25
e2.Name = "yuio"
t.Log(e)
t.Log(e1)
t.Log(e1.Id)
t.Log(e2)
t.Logf("e is #{e}")
t.Logf("e2 is #{e2}")
/*func (e *Employee) String() string {
fmt.Printf("Adress is %x", unsafe.Pointer(&e.Name))
return fmt.Sprintf("ID:%s/Name:%s/Age:%d", e.Id, e.Name, e.Age)
}*/
/** result
=== RUN TestCreateEmployeeObject
struct_test.go:32: {11 liSi 26}
struct_test.go:33: { wangWu 21}
struct_test.go:34:
Adress is c00012a400 struct_test.go:35: ID:3/Name:yuio/Age:25
struct_test.go:36: e is #{e}
struct_test.go:37: e2 is #{e2}
--- PASS: TestCreateEmployeeObject (0.00s)
PASS
PASS
*/
/*func (e Employee) String() string {
fmt.Printf("Address is %x\n", unsafe.Pointer(&e.Name))
return fmt.Sprintf("ID:%s-Name:%s-Age:%d", e.Id, e.Name, e.Age)
}*/
/** result
=== RUN TestCreateEmployeeObject
Address is c0000787f0
struct_test.go:32: ID:11-Name:liSi-Age:26
Address is c0000788b0
struct_test.go:33: ID:-Name:wangWu-Age:21
struct_test.go:34:
Address is c0000788e0
struct_test.go:35: ID:3-Name:yuio-Age:25
struct_test.go:36: e is #{e}
struct_test.go:37: e2 is #{e2}
--- PASS: TestCreateEmployeeObject (0.00s)
PASS
*/
}
func TestStructOperations(t *testing.T) {
e := Employee{
"0", "yuio", 26}
fmt.Printf("Address is %x\n", unsafe.Pointer(&e.Name))
t.Log(e.String())
/**
=== RUN TestStructOperations
Address is c00012a400
Adress is c00012a400 struct_test.go:78: ID:0/Name:yuio/Age:26
--- PASS: TestStructOperations (0.00s)
PASS
*/
}
interface
Go interface
Differences from other major programming languages
- The interface is non-invasive and the implementation does not depend on the interface definition
- So the definition of the interface can be included in the interface consumer package
interface variable
package _interface
import "testing"
// 定义一个interface
type Programmer interface {
WriteHelloWorld() string
}
// 定义一个实体类
type GoProgrammer struct {
}
// 实现接口方法impl
func (g *GoProgrammer) WriteHelloWorld() string {
return "fmt.Println(\"Hello World\")"
}
func TestClient(t *testing.T) {
var p Programmer
p = new(GoProgrammer)
t.Log(p.WriteHelloWorld())
/**
=== RUN TestClient
interface_test.go:21: fmt.Println("Hello World")
--- PASS: TestClient (0.00s)
PASS
*/
}
PS: study notes, invade and delete!