go print "%+v" 可以打印私有变量;reflect遍历struct则不能

Python028

go print "%+v" 可以打印私有变量;reflect遍历struct则不能,第1张

go print函数“%+v”可以访问私有变量(如果私有变量里有map,打印相当于读,会有map并发读写问题)

比如http ctx里,就会有所有连接的map,打印ctx会有并发读写问题。因此,需要使用context标准方法Value

reflect遍历struct却不可以

tree

main.go

user.go

私有成员变量在别的类中是无法访问的,要不还叫什么私有。

不过子类可以通过设置器和访问器来访问父类的私有成员变量。

public class A

{

private String name

private int age

public String getName() {

return name

}

public void setName(String name) {

this.name = name

}

public int getAge() {

return age

}

public void setAge(int age) {

this.age = age

}

public void show()

{

System.out.println(this.getName()+","+this.getAge())

}

}

public class B extends A

{

public static void main(String[] args)

{

B b=new B()

b.setName("李三")

b.setAge(19)

b.show()

}

}