1. java代码
public class App {
public static void main(String[] args) {
Animal a = new Animal("dog");
a.talk();
a.talk();
}
}
class Animal {
String name;
public Animal(String name) {
this.name = name;
}
public void talk() {
System.out.println(this.name);
this.name = "animal";
}
}
----运行结果------------------------------
dog
animal
2. go代码
2.1. 结构体调用
package main
import (
"fmt"
)
type Animal struct {
name string
}
func (this Animal) Talk() {
fmt.Printf("This<%p> is a %s\n", &this, this.name)
this.name = "animal"
}
func main() {
var a = Animal{"dog"}
a.Talk()
a.Talk()
}
----运行结果------------------------------
This<0xc00003c250> is a dog
This<0xc00003c270> is a dog
2.2. 指针调用
package main
import (
"fmt"
)
type Animal struct {
name string
}
func (this *Animal) Talk() {
fmt.Printf("This<%p> is a %s\n", this, this.name)
this.name = "animal"
}
func main() {
var a = Animal{"dog"}
a.Talk()
a.Talk()
}
----运行结果------------------------------
This<0xc00003e250> is a dog
This<0xc00003e250> is a animal
3. 结构体与指针区别
go tool compile -S example.go
结构体形式, 第11行, 调用runtime.newobject(SB)
, 新建实例
0x0000 00000 (example.go:11) TEXT “”.Animal.Talk(SB), ABIInternal, $104-16
0x0000 00000 (example.go:11) CMPQ SP, 16(R14)
0x0004 00004 (example.go:11) PCDATA $0, $-2
0x0004 00004 (example.go:11) JLS 257
0x000a 00010 (example.go:11) PCDATA $0, $-1
0x000a 00010 (example.go:11) SUBQ $104, SP
0x000e 00014 (example.go:11) MOVQ BP, 96(SP)
0x0013 00019 (example.go:11) LEAQ 96(SP), BP
0x0018 00024 (example.go:11) FUNCDATA $0, gclocals·533adcd55fa5ed3e2fd959716125aef9(SB)
0x0018 00024 (example.go:11) FUNCDATA $1, gclocals·3742fd8e0787ad015675be6528a75f13(SB)
0x0018 00024 (example.go:11) FUNCDATA $2, “”.Animal.Talk.stkobj(SB)
0x0018 00024 (example.go:11) FUNCDATA $5, “”.Animal.Talk.arginfo1(SB)
0x0018 00024 (example.go:11) MOVQ AX, “”.this+112(SP)
0x001d 00029 (example.go:11) MOVQ BX, “”.this+120(SP)
0x0022 00034 (example.go:11) LEAQ type.“”.Animal(SB), AX
0x0029 00041 (example.go:11) PCDATA $1, $0
0x0029 00041 (example.go:11) CALL runtime.newobject(SB)
0x002e 00046 (example.go:11) MOVQ AX, “”.&this+56(SP)
0x0033 00051 (example.go:11) PCDATA $0, $-2
0x0033 00051 (example.go:11) CMPL runtime.writeBarrier(SB), $0
0x003a 00058 (example.go:11) JNE 70
0x003c 00060 (example.go:11) MOVUPS “”.this+112(SP), X0
0x0041 00065 (example.go:11) MOVUPS X0, (AX)
0x0044 00068 (example.go:11) JMP 95
0x0046 00070 (example.go:11) MOVQ AX, BX
0x0049 00073 (example.go:11) LEAQ “”.this+112(SP), CX
0x004e 00078 (example.go:11) LEAQ type.“”.Animal(SB), AX
0x0055 00085 (example.go:11) CALL runtime.typedmemmove(SB)
指标形式, 无新建实例操作
0x0000 00000 (example.go:11) TEXT “”.(*Animal).Talk(SB), ABIInternal, $96-8
0x0000 00000 (example.go:11) CMPQ SP, 16(R14)
0x0004 00004 (example.go:11) PCDATA $0, $-2
0x0004 00004 (example.go:11) JLS 191
0x000a 00010 (example.go:11) PCDATA $0, $-1
0x000a 00010 (example.go:11) SUBQ $96, SP
0x000e 00014 (example.go:11) MOVQ BP, 88(SP)
0x0013 00019 (example.go:11) LEAQ 88(SP), BP
0x0018 00024 (example.go:11) FUNCDATA $0, gclocals·c7c4fc7b12f6707ea74acf7400192967(SB)
0x0018 00024 (example.go:11) FUNCDATA $1, gclocals·e74db6c75a94ea7c4aff062dedbdd62f(SB)
0x0018 00024 (example.go:11) FUNCDATA $2, “”.(*Animal).Talk.stkobj(SB)
0x0018 00024 (example.go:11) FUNCDATA $5, “”.(*Animal).Talk.arginfo1(SB)
0x0018 00024 (example.go:11) FUNCDATA $6, “”.(*Animal).Talk.argliveinfo(SB)
0x0018 00024 (example.go:11) PCDATA $3, $1