�ӿڱ���������Ҳ����ʹ��һ��������ʽ�� swtich ����⣺type-swtich �������� ʾ�� 11.4 �ĵڶ����֣���
switch t := areaIntf.(type) {
case *Square:
fmt.Printf("Type Square %T with value %v\n", t, t)
case *Circle:
fmt.Printf("Type Circle %T with value %v\n", t, t)
case nil:
fmt.Printf("nil value: nothing to check?\n")
default:
fmt.Printf("Unexpected type %T\n", t)
}
�����
Type Square *main.Square with value &{5}
���� t �õ��� areaIntf ��ֵ�����ͣ� ���� case ������оٵ����ͣ�nil ���⣩������ʵ�ֶ�Ӧ�Ľӿڣ��������м� Shaper����������������û���� case ����оٵ������У��ͻ�ִ��default ��䡣
������ type-switch ��������ʱ���ͷ����������� type-switch �������� fallthrough ��
��������Dz��Ա��������ͣ���������ֵ����ô�Ϳ��Բ���Ҫ��ֵ��䣬���磺
switch areaIntf.(type) {
case *Square:
// TODO
case *Circle:
// TODO
...
default:
// TODO
}
����Ĵ���Ƭ��չʾ��һ�����ͷ��ຯ��������һ���ɱ䳤�Ȳ������������������͵����飬�����������Ԫ�ص�ʵ������ִ�в�ͬ�Ķ�����
func classifier(items ...interface{}) {
for i, x := range items {
switch x.(type) {
case bool:
fmt.Printf("Param #%d is a bool\n", i)
case float64:
fmt.Printf("Param #%d is a float64\n", i)
case int, int64:
fmt.Printf("Param #%d is a int\n", i)
case nil:
fmt.Printf("Param #%d is a nil\n", i)
case string:
fmt.Printf("Param #%d is a string\n", i)
default:
fmt.Printf("Param #%d is unknown\n", i)
}
}
}
�����������ô˷�����classifier(13, -14.3, "BELGIUM", complex(1, 2), nil, false) ��
�ڴ����������ⲿ�ġ�����δ֪������ʱ������������� JSON �� XML ��������ݣ����Ͳ��Ժ�ת����dz����á�
�� ʾ��12.17(xml.go) �н��� XML �ĵ��ǣ����Ǿͻ��õ� type-switch ��
��ϰ 11.4��simple_interface2.go
���� ��ϰ 11.1 �е����ݣ������ڶ������� RSimple����Ҳʵ���˽ӿ� Simpler��дһ������ fi������������ Simple �� RSimple ���͵ı�����