2012년 4월 24일 화요일

switch

switch 문.

문법설명은 필요없다.

한줄의 코드를 읽는것이 언어를 배우는 가장 빠른방법이다.




GO 언어의 switch 문은 상수식뿐만아니라, 논리식도 평가할 수 있게되어,
그 표현력이 풍부해졌다.


// Compare returns an integer comparing the two byte arrays,
// lexicographically.
// The result will be 0 if a == b, -1 if a < b, and +1 a > b


func Compare(a, b []byte) int {
for i := 0; i < len(a) && i < len(b); i++ {
switch {
case a[i] > b[i]:
return 1
case a[i] < b[i]:
return -1
}
}
switch {
case len(a) < len(b):
return -1
case len(a) > len(b):
return 1
}
return 0
}