1. func myAtoi(str string) int {
    2. bytes := []byte(str)
    3. symbol := 1
    4. var arr []byte
    5. i := 0
    6. for;i < len(bytes) && bytes[i] == ' ';i++ {}
    7. if i < len(bytes) {
    8. if bytes[i] == '+' {
    9. symbol = 1
    10. i++
    11. } else if bytes[i] == '-' {
    12. symbol = -1
    13. i++
    14. }
    15. }
    16. for;i < len(bytes) && bytes[i] >='0' && bytes[i] <= '9'; i++ {
    17. arr = append(arr, bytes[i])
    18. }
    19. if len(arr) != 0 {
    20. result := int64(0)
    21. max := int64(math.MaxInt32)
    22. if symbol < 0 {
    23. max = -int64(math.MinInt32)
    24. }
    25. for j := 0; j < len(arr); j++ {
    26. b := int64(arr[j] - '0')
    27. result = result * 10 + b
    28. if result > max {
    29. if symbol > 0 {
    30. return math.MaxInt32
    31. } else {
    32. return math.MinInt32
    33. }
    34. }
    35. }
    36. if symbol < 0 {
    37. result = -result
    38. }
    39. return int(result)
    40. }
    41. return 0
    42. }
    43. func main() {
    44. fmt.Println(myAtoi("+42"))
    45. fmt.Println(myAtoi("-42"))
    46. fmt.Println(myAtoi("-042"))
    47. fmt.Println(myAtoi(" -42"))
    48. fmt.Println(myAtoi("4193 with words"))
    49. fmt.Println(myAtoi("words and 987"))
    50. fmt.Println(myAtoi("-91283472332"))
    51. fmt.Println(myAtoi("9223372036854775808"))
    52. }