Go基础(3)
demo1:
package main import "fmt" func print() { for i := 1; i < 10; i++ { for j := 1; j <= i; j++ { fmt.Printf("%d * %d = %d ", j, i, i*j) } fmt.Println() } } func main() { print() }
分析:
1.基本的99乘法表打印练习
demo2:
package main import "fmt" func main() { const min int = 1 const max int = 10000 process(min, max) } func process(min, max int) { for i := min; i <= max; i++ { if perfect(i) { fmt.Printf("%d是完数\n", i) } } } func perfect(n int) bool { var sum int = 0 for i := 1; i < n; i++ { if n%i == 0 { sum += i } } return n == sum }
分析:
1.判断1-10000有哪些完数的练习
demo3:
package main import "fmt" func main() { const str1 string = "helloolleh" const str2 string = "helloworld" process(str1) process(str2) const str3 string = "上海自来水来自海上" const str4 string = "我喜欢学习Go" const str5 string = "go上海自来水来自海上og" essential(str3) essential(str4) essential(str5) } //可以正常处理英文字符串,无法处理中文 func process(str string) { var flag bool = true for i := 0; i < len(str); i++ { if i == len(str)/2 { break } last := len(str) - i - 1 if str[i] != str[last] { flag = false } } if flag { fmt.Printf("%s 是回文字符串\n", str) } else { fmt.Printf("%s 不是回文字符串\n", str) } return } //正常处理所有的字符串 func essential(str string) { tmp := []rune(str) lenth := len(tmp) var flag bool = true for i, _ := range tmp { if i == lenth/2 { break } last := lenth - i - 1 if tmp[i] != tmp[last] { flag = false } } if flag { fmt.Printf("%s 是回文字符串\n", str) } else { fmt.Printf("%s 不是回文字符串\n", str) } return }
分析:
1.判断是否是回文字符串的练习
2.尤其注意中文字符的处理,使用rune
demo4:
package main import ( "bufio" "fmt" "os" ) func main() { result := read() worldCount, spaceCount, numberCount, otherCount := count(result) fmt.Printf("字母:%d\n空格:%d\n数字:%d\n其他字符:%d\n", worldCount, spaceCount, numberCount, otherCount) } func read() string { reader := bufio.NewReader(os.Stdin) result, _, err := reader.ReadLine() if err != nil { fmt.Println("read from console error:", err) return "" } return string(result) } func count(str string) (worldCount, spaceCount, numberCount, otherCount int) { tmp := []rune(str) for _, v := range tmp { switch { case v >= 'a' && v <= 'z': fallthrough case v >= 'A' && v <= 'Z': worldCount++ case v == ' ': spaceCount++ case v >= '0' && v <= '9': numberCount++ default: otherCount++ } } return }
分析:
1.读取一串输入并对字符进行技术的程序
2.对常用其他包的简单使用
demo5:
package main import "fmt" func main() { var a string = "92233720368547758075" var b string = "129223372036854775807" result := bigIntegerAdd(a, b) fmt.Printf("%s + %s = %s\n", a, b, result) } func bigIntegerAdd(a, b string) (result string) { if len(a) == 0 || len(b) == 0 { result = "0" return } var index1 int = len(a) - 1 var index2 int = len(b) - 1 var left int for index1 >= 0 && index2 >= 0 { c1 := a[index1] - '0' c2 := b[index2] - '0' sum := int(c1) + int(c2) + left if sum >= 10 { left = 1 } else { left = 0 } c3 := (sum % 10) + '0' result = fmt.Sprintf("%c%s", c3, result) index1-- index2-- } for index1 >= 0 { c1 := a[index1] - '0' sum := int(c1) + left if sum >= 10 { left = 1 } else { left = 0 } c3 := (sum % 10) + '0' result = fmt.Sprintf("%c%s", c3, result) index1-- } for index2 >= 0 { c2 := b[index2] - '0' sum := int(c2) + left if sum >= 10 { left = 1 } else { left = 0 } c3 := (sum % 10) + '0' result = fmt.Sprintf("%c%s", c3, result) index2-- } if left == 1 { result = fmt.Sprintf("1%s", result) } return }
分析:
1.Go实现大数相加,面试常见问题
2.实现思想:人的思维,从个位依此加到最高位
demo6:
package main import "fmt" func test() int { defer func() { if err := recover(); err != nil { fmt.Println(err) } }() b := 0 a := 100 / b return a } func main() { test() }
分析:
1.Go没有try,catch语句,应该用这里的方式
demo7:
package main import "fmt" func test() { i := new(int) fmt.Println(i) //0xc000012088 fmt.Println(*i) //0 s1 := new([]int) *s1 = make([]int, 5) fmt.Println(s1) //&[0 0 0 0 0] s2 := make([]int, 5) fmt.Println(s2) //[0 0 0 0 0] (*s1)[0] = 100 s2[0] = 100 fmt.Println(s1) //&[100 0 0 0 0] fmt.Println(s2) //&[100 0 0 0 0] } func main() { test() }
分析:
1.make和new的区别练习