diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/01/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/01/main.go" new file mode 100644 index 000000000..7ed081586 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/01/main.go" @@ -0,0 +1,33 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + // 当一个整数和一个浮点数一起使用时 + // 在一个表达式中,其结果总是变成一个浮点数 + fmt.Println(8 * -4.0) // -32.0 not -32 + + // 两个整数值的结果是一个整数值 + fmt.Println(-4 / 2) + + // 取余运算符 + // 它只能用于整数 + fmt.Println(5 % 2) + // fmt.Println(5.0 % 2) // 错误的 + + // 加法运算符 + fmt.Println(1 + 2.5) + fmt.Println(2 - 3) + + // 负数运算符 + fmt.Println(-(-2)) + fmt.Println(- -2) // 这也可以 +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/02/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/02/main.go" new file mode 100644 index 000000000..c4089061a --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/02/main.go" @@ -0,0 +1,23 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + var ( + myAge = 30 + yourAge = 35 + average float64 + ) + + average = float64(myAge+yourAge) / 2 + + fmt.Println(average) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/03-float-inaccuracy/01/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/03-float-inaccuracy/01/main.go" new file mode 100644 index 000000000..9e0dcf9a8 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/03-float-inaccuracy/01/main.go" @@ -0,0 +1,26 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + ratio := 1.0 / 10.0 + + // 经过10次操作后 + // 不准确的地方很明显 + // + // 不过,现在不要介意这个循环的语法 + // 我将在事后解释 + for range [...]int{10: 0} { + ratio += 1.0 / 10.0 + } + + fmt.Printf("%.60f", ratio) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/03-float-inaccuracy/02/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/03-float-inaccuracy/02/main.go" new file mode 100644 index 000000000..a57743bb6 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/03-float-inaccuracy/02/main.go" @@ -0,0 +1,24 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + // Go编译器将这些数字视为整数, + // 因为整数值中没有小数部分 + // 所以, 结果是1, 而不是1.5 + + // 所以, 这里的ratio变量是一个int变量 + // 这是因为, 3除以2的结果是一个整数 + + ratio := 3 / 2 + + fmt.Printf("%d", ratio) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/03-float-inaccuracy/03/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/03-float-inaccuracy/03/main.go" new file mode 100644 index 000000000..a639a6d1e --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/01-arithmetic-operators/03-float-inaccuracy/03/main.go" @@ -0,0 +1,27 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + // 当你在计算中使用一个浮点数和一个整数时 + // 结果总是一个浮点数。 + + ratio := 3.0 / 2 + + // OR: + // ratio = 3 / 2.0 + + // OR - 如果3在一个int变量里: + // n := 3 + // ratio = float64(n) / 2 + + fmt.Printf("%f", ratio) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/02-arithmetic-operators-examples/01/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/02-arithmetic-operators-examples/01/main.go" new file mode 100644 index 000000000..a941230ca --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/02-arithmetic-operators-examples/01/main.go" @@ -0,0 +1,31 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + fmt.Println("sum :", 3+2) // sum - int + fmt.Println("sum :", 2+3.5) // sum - float64 + fmt.Println("dif :", 3-1) // difference - int + fmt.Println("dif :", 3-0.5) // difference - float64 + fmt.Println("prod:", 4*5) // product - int + fmt.Println("prod:", 5*2.5) // product - float64 + fmt.Println("quot:", 8/2) // quotient - int + fmt.Println("quot:", 8/1.5) // quotient - float64 + + // 余数仅适用于整数 + fmt.Println("rem :", 8%3) + // fmt.Println("rem:", 8.0%3) // error + + // 你可以这样做 + // 因为浮点数的小数部分为零 + f := 8.0 + fmt.Println("rem :", int(f)%3) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/02-arithmetic-operators-examples/02/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/02-arithmetic-operators-examples/02/main.go" new file mode 100644 index 000000000..98673302a --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/02-arithmetic-operators-examples/02/main.go" @@ -0,0 +1,34 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + // 这个ratio的值是什么? + // 3 / 2 = 1.5? + var ratio float64 = 3 / 2 + fmt.Println(ratio) + + // 说明 + // 上述表达式等于这个 + ratio = float64(int(3) / int(2)) + fmt.Println(ratio) + + // 如何修复? + // + // 记住,当其中一个值是浮点数的时候 + // 结果就会变成浮点数 + ratio = float64(3) / 2 + fmt.Println(ratio) + + // or + ratio = 3.0 / 2 + fmt.Println(ratio) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/03-precedence/01/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/03-precedence/01/main.go" new file mode 100644 index 000000000..18f935d0a --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/03-precedence/01/main.go" @@ -0,0 +1,28 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + fmt.Println( + 2+2*4/2, + 2+((2*4)/2), // 同上 + ) + + fmt.Println( + 1+4-2, + (1+4)-2, // 同上 + ) + + fmt.Println( + (2+2)*4/2, + (2+2)*(4/2), // 同上 + ) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/03-precedence/02/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/03-precedence/02/main.go" new file mode 100644 index 000000000..4d24d3097 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/03-precedence/02/main.go" @@ -0,0 +1,21 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + n, m := 1, 5 + + fmt.Println(2 + 1*m/n) + fmt.Println(2 + ((1 * m) / n)) // 同上 + + // 让我们用括号来改变优先顺序 + fmt.Println(((2 + 1) * m) / n) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/03-precedence/03/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/03-precedence/03/main.go" new file mode 100644 index 000000000..42ecbc07d --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/03-precedence/03/main.go" @@ -0,0 +1,31 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +func main() { + // // 优先级: 表达式的顺序 + // // 乘法运算符优先运行: * and / + + // fmt.Println( + // 1 + 5 - 3*10/2, + // ) + + // // 3 * 10 = 30 + // // 30 / 2 = 15 + // // 1 + 5 = 6 + // // 6 - 15 = -9 + + // // **** TIP **** + // // 使用括号来改变顺序 + // // 首先, (1+5-3), 然后 (10/2) 将被计算出来 + + // fmt.Println( + // (1 + 5 - 3) * (10 / 2), + // ) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/03-precedence/04/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/03-precedence/04/main.go" new file mode 100644 index 000000000..6c46fc9f9 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/03-precedence/04/main.go" @@ -0,0 +1,21 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + celsius := 35. + + // 错误的公式: 9*celsius + 160 / 5 + // 正确的公式: (9*celsius + 160) / 5 + fahrenheit := (9*celsius + 160) / 5 + + fmt.Printf("%g ºC is %g ºF\n", celsius, fahrenheit) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/04-incdec-statement/01/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/04-incdec-statement/01/main.go" new file mode 100644 index 000000000..688bfa5cc --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/04-incdec-statement/01/main.go" @@ -0,0 +1,24 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + var n int + + // 替代方案: + // n = n + 1 + // n += 1 + + // 较好的: + n++ + + fmt.Println(n) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/04-incdec-statement/02/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/04-incdec-statement/02/main.go" new file mode 100644 index 000000000..074e3fe25 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/04-incdec-statement/02/main.go" @@ -0,0 +1,24 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + n := 10 + + // 替代方案: + // n = n - 1 + // n -= 1 + + // 较好的: + n-- + + fmt.Println(n) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/04-incdec-statement/03/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/04-incdec-statement/03/main.go" new file mode 100644 index 000000000..ccb3536c4 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/04-incdec-statement/03/main.go" @@ -0,0 +1,31 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +// 自增自减运算符是一个语句 + +func main() { + var counter int + + // 以下的 "语句" 是正确的: + + counter++ // 1 + counter++ // 2 + counter++ // 3 + counter-- // 2 + fmt.Printf("There are %d line(s) in the file\n", + counter) + + // 以下的 "表达式" 是不正确的: + + // counter = 5+counter-- + // counter = ++counter + counter-- +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/05-assignment-operations/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/05-assignment-operations/main.go" new file mode 100644 index 000000000..eeb7fb801 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/05-assignment-operations/main.go" @@ -0,0 +1,42 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" +) + +func main() { + width, height := 5., 12. + + // 计算长方形的面积 + area := width * height + fmt.Printf("%gx%g=%g\n", width, height, area) + + area = area - 10 // area减10 + area = area + 10 // area加10 + area = area * 2 // area乘以2 + area = area / 2 // area除以2 + fmt.Printf("area=%g\n", area) + + // // 赋值运算符 + area -= 10 // area减10 + area += 10 // area加10 + area *= 2 // area乘以2 + area /= 2 // area除以2 + fmt.Printf("area=%g\n", area) + + // 找出 area 变量的余数 + // 因为: area 是浮点数, 所以这不起作用: + // area %= 7 + + // 这种方法可以运作 + area = float64(int(area) % 7) + fmt.Printf("area=%g\n", area) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/06-project-feet-to-meters/exercise-solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/06-project-feet-to-meters/exercise-solution/main.go" new file mode 100644 index 000000000..b425237ec --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/06-project-feet-to-meters/exercise-solution/main.go" @@ -0,0 +1,26 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "os" + "strconv" +) + +func main() { + c, _ := strconv.ParseFloat(os.Args[1], 64) + f := c*1.8 + 32 + + // 这样: + fmt.Printf("%g ºC is %g ºF\n", c, f) + + // 或者像这样(两者都是正确的): + fmt.Printf("%g ºF\n", f) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/06-project-feet-to-meters/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/06-project-feet-to-meters/main.go" new file mode 100644 index 000000000..5380295b6 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/06-project-feet-to-meters/main.go" @@ -0,0 +1,30 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "os" + "strconv" +) + +func main() { + arg := os.Args[1] + + // feet 现在是 float64 + feet, _ := strconv.ParseFloat(arg, 64) + + meters := feet * 0.3048 + + fmt.Printf("%f feet is %f meters.\n", feet, meters) + + // 漂亮的打印出来: + + // fmt.Printf("%g feet is %g meters.\n", feet, meters) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/01-do-some-calculations/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/01-do-some-calculations/main.go" new file mode 100644 index 000000000..1bdb99b54 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/01-do-some-calculations/main.go" @@ -0,0 +1,31 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +// --------------------------------------------------------- +// 练习: 做一些计算 +// +// 1. 打印 50 和 25 的总和 +// 2. 打印 50 和 15.5 的差值 +// 3. 打印 50 和 0.5 的乘积 +// 4. 打印 50 和 0.5 的商数 +// 5. 打印 25 和 3 的余数 +// 6. 打印 `5 + 2` 的负数 +// +// 预期输出 +// 75 +// 34.5 +// 25 +// 100 +// 1 +// -7 +// --------------------------------------------------------- + +func main() { +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/01-do-some-calculations/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/01-do-some-calculations/solution/main.go" new file mode 100644 index 000000000..261a510ea --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/01-do-some-calculations/solution/main.go" @@ -0,0 +1,20 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + fmt.Println(50 + 25) + fmt.Println(50 - 15.5) + fmt.Println(50 * 0.5) + fmt.Println(50 / 0.5) + fmt.Println(25 % 3) + fmt.Println(-(5 + 2)) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/02-fix-the-float/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/02-fix-the-float/main.go" new file mode 100644 index 000000000..c9f0167d2 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/02-fix-the-float/main.go" @@ -0,0 +1,25 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +// --------------------------------------------------------- +// 练习: 修复浮点数 +// +// 修复程序以打印 2.5 而不是 2 +// +// 预期输出 +// 2.5 +// --------------------------------------------------------- + +func main() { + x := 5 / 2 + fmt.Println(x) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/02-fix-the-float/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/02-fix-the-float/solution/main.go" new file mode 100644 index 000000000..6567a12d7 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/02-fix-the-float/solution/main.go" @@ -0,0 +1,21 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + // 以下方案是正确的: + x := 5. / 2 + // x := 5 / 2. + // x := float64(5) / 2 + // x := 5 / float64(2) + + fmt.Println(x) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/03-precedence/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/03-precedence/main.go" new file mode 100644 index 000000000..0e78db0f8 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/03-precedence/main.go" @@ -0,0 +1,44 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +// --------------------------------------------------------- +// 练习: 优先级 +// +// 改变表达式以产生预期输出 +// +// 限制 +// 使用括号来改变优先顺序 +// --------------------------------------------------------- + +func main() { + // 这个表达式应该打印 20 + fmt.Println(10 + 5 - 5 - 10) + + // 这个表达式应该打印 -16 + fmt.Println(-10 + 0.5 - 1 + 5.5) + + // 这个表达式应该打印 -25 + fmt.Println(5 + 10*2 - 5) + + // 这个表达式应该打印 0.5 + fmt.Println(0.5*2 - 1) + + // 这个表达式应该打印 24 + fmt.Println(3 + 1/2*10 + 4) + + // 这个表达式应该打印 15 + fmt.Println(10 / 2 * 10 % 7) + + // 这个表达式应该打印 40 + // 请注意,你可能需要使用浮点数来解决这个问题 + fmt.Println(100 / 5 / 2) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/03-precedence/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/03-precedence/solution/main.go" new file mode 100644 index 000000000..ed04470a1 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/03-precedence/solution/main.go" @@ -0,0 +1,38 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + // 10 + 5 - 5 - 10 + fmt.Println(10 + 5 - (5 - 10)) + + // -10 + 0.5 - 1 + 5.5 + fmt.Println(-10 + 0.5 - (1 + 5.5)) + + // 5 + 10*2 - 5 + fmt.Println(5 + 10*(2-5)) + + // 0.5*2 - 1 + fmt.Println(0.5 * (2 - 1)) + + // 3 + 1/2*10 + 4 + fmt.Println((3+1)/2*10 + 4) + + // 10 / 2 * 10 % 7 + fmt.Println(10 / 2 * (10 % 7)) + + // 100 / 5 / 2 + // 5 / 2 = 2 + // 5 和 2 是整数, 所以, 刪除小数部分 + // 5. / 2 = 2.5 + // 因为 5 是一个浮点数,所以结果变成了一个浮点数 + fmt.Println(100 / (5. / 2)) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/04-incdecs/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/04-incdecs/main.go" new file mode 100644 index 000000000..b9d8e7427 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/04-incdecs/main.go" @@ -0,0 +1,34 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +// --------------------------------------------------------- +// 练习: 自增自减运算符 +// +// 1. 将 `counter` 增加 5 倍 +// 2. 将 `factor` 减少 2 倍 +// 3. 打印 counter 和 factor 的乘积 +// +// 限制 +// 只使用自增自减运算符语句 +// +// 预期输出 +// -75 +// --------------------------------------------------------- + +func main() { + // 别碰这个 + counter, factor := 45, 0.5 + + // 在下面输入你的代码 + // ... + + // 最后: 删除下面的代码 + _, _ = counter, factor +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/04-incdecs/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/04-incdecs/solution/main.go" new file mode 100644 index 000000000..c5c63e2c1 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/04-incdecs/solution/main.go" @@ -0,0 +1,26 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + counter, factor := 45, 0.5 + + counter++ + counter++ + counter++ + counter++ + counter++ + + factor-- + factor-- + + fmt.Println(float64(counter) * factor) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/05-manipulate-a-counter/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/05-manipulate-a-counter/main.go" new file mode 100644 index 000000000..bbdf5ed0e --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/05-manipulate-a-counter/main.go" @@ -0,0 +1,38 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +// --------------------------------------------------------- +// 练习: 计数器操作 +// +// 1. 写出最简单的一行代码,使counter变量增加1。 +// +// 2. 写出最简单的一行代码,使counter变量减少1。 +// +// 3. 写出最简单的一行代码,使counter变量增加5。 +// +// 4. 写出最简单的一行代码,使counter变量乘以10。 +// +// 5. 写一行最简单的代码,把counter变量除以5。 +// +// 预期输出 +// 10 +// --------------------------------------------------------- + +func main() { + // 不要修改下面的代码 + var counter int + + // 在这里输入你的代码 + + // 不要修改下面的代码 + fmt.Println(counter) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/05-manipulate-a-counter/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/05-manipulate-a-counter/solution/main.go" new file mode 100644 index 000000000..dbceea69e --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/05-manipulate-a-counter/solution/main.go" @@ -0,0 +1,24 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + var counter int + + counter++ + counter-- + + counter += 5 + counter *= 10 + counter /= 5 + + fmt.Println(counter) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/06-simplify-the-assignments/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/06-simplify-the-assignments/main.go" new file mode 100644 index 000000000..fea8c1762 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/06-simplify-the-assignments/main.go" @@ -0,0 +1,37 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +// --------------------------------------------------------- +// 练习: 简化赋值运算符 +// +// 简化代码(重构) +// +// 限制 +// 只使用自增自减运算符和赋值运算符 +// +// 预期输出 +// 3 +// --------------------------------------------------------- + +func main() { + width, height := 10, 2 + + width = width + 1 + width = width + height + width = width - 1 + width = width - height + width = width * 20 + width = width / 25 + width = width % 5 + + fmt.Println(width) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/06-simplify-the-assignments/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/06-simplify-the-assignments/solution/main.go" new file mode 100644 index 000000000..ad5fb1dda --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/06-simplify-the-assignments/solution/main.go" @@ -0,0 +1,25 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + width, height := 10, 2 + + width++ + width += height + width-- + width -= height + width *= 20 + width /= 25 + width %= 5 + + fmt.Println(width) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/07-circle-area/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/07-circle-area/main.go" new file mode 100644 index 000000000..2032ec50f --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/07-circle-area/main.go" @@ -0,0 +1,50 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" +) + +// --------------------------------------------------------- +// 练习: 圆面积 +// +// 根据给定的半径(radius)计算出圆的面积 +// +// 圆面积计算公式 +// area = πr² +// https://en.wikipedia.org/wiki/Area#Circles +// +// 提示 +// 对于 PI 你可以使用 `math.Pi` +// +// 预期输出 +// radius: 10 -> area: 314.1592653589793 +// +// BONUS 练习! +// 1. 将 area 打印为 314.16 +// 2. 要做到这一点,你需要使用正确的Printf动词 :) +// 而不是下面的 `%g` 动词. +// +// 预期输出 +// radius: 10 -> area: 314.16 +// --------------------------------------------------------- + +func main() { + var ( + radius = 10. + area float64 + ) + + // 在这里添加你的代码 + // ... + + // 别碰这个 + fmt.Printf("radius: %g -> area: %g\n", radius, area) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/07-circle-area/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/07-circle-area/solution/main.go" new file mode 100644 index 000000000..888dac3f8 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/07-circle-area/solution/main.go" @@ -0,0 +1,30 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "math" +) + +func main() { + var ( + radius = 10. + area float64 + ) + + area = math.Pi * radius * radius + + fmt.Printf("radius: %g -> area: %.2f\n", + radius, area) + + // 替代方案: + // math.Pow 计算一个浮点数的幂 + // area = math.Pi * math.Pow(radius, 2) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/08-sphere-area/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/08-sphere-area/main.go" new file mode 100644 index 000000000..0c9d9c8b7 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/08-sphere-area/main.go" @@ -0,0 +1,47 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" +) + +// --------------------------------------------------------- +// 练习: 球体面积 +// +// 1. 从命令行获取半径 +// 2. 将其转换为 float64 +// 3. 计算球体的表面积 +// +// 球体表面积公式 +// area = 4πr² +// https://en.wikipedia.org/wiki/Sphere#Surface_area +// +// 限制 +// 使用 `math.Pow` 来计算面积 +// 阅读文档看看它是如何计算的. +// https://golang.org/pkg/math/#Pow +// +// 预期输出 +// go run main.go 10 +// 1256.64 +// +// go run main.go 54.2 +// 36915.47 +// --------------------------------------------------------- + +func main() { + var radius, area float64 + + // 在这里添加你的代码 + // ... + + // 别碰这个 + fmt.Printf("radius: %g -> area: %.2f\n", radius, area) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/08-sphere-area/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/08-sphere-area/solution/main.go" new file mode 100644 index 000000000..0b99284a9 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/08-sphere-area/solution/main.go" @@ -0,0 +1,27 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "math" + "os" + "strconv" +) + +func main() { + var radius, area float64 + + radius, _ = strconv.ParseFloat(os.Args[1], 64) + + area = 4 * math.Pi * math.Pow(radius, 2) + + fmt.Printf("radius: %g -> area: %.2f\n", + radius, area) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/09-sphere-volume/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/09-sphere-volume/main.go" new file mode 100644 index 000000000..611acbec8 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/09-sphere-volume/main.go" @@ -0,0 +1,44 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" +) + +// --------------------------------------------------------- +// 练习: 球体体积 +// +// 1. 从命令行获取半径 +// 2. 将其转换为 float64 +// 3. 计算球体的体积 +// +// 球体体积公式 +// https://en.wikipedia.org/wiki/Sphere#Enclosed_volume +// +// 限制 +// 使用 `math.Pow` 来计算体积 +// +// 预期输出 +// go run main.go 10 +// 4188.79 +// +// go run main.go .5 +// 0.52 +// --------------------------------------------------------- + +func main() { + var radius, vol float64 + + // 在这里添加你的代码 + // ... + + // 别碰这个 + fmt.Printf("radius: %g -> volume: %.2f\n", radius, vol) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/09-sphere-volume/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/09-sphere-volume/solution/main.go" new file mode 100644 index 000000000..7f2926909 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/09-sphere-volume/solution/main.go" @@ -0,0 +1,26 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "math" + "os" + "strconv" +) + +func main() { + var radius, vol float64 + + radius, _ = strconv.ParseFloat(os.Args[1], 64) + + vol = (4 * math.Pi * math.Pow(radius, 3)) / 3 + + fmt.Printf("radius: %g -> volume: %.2f\n", radius, vol) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/README.md" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/README.md" new file mode 100644 index 000000000..c17d33fe9 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\347\273\203\344\271\240/README.md" @@ -0,0 +1,19 @@ +# 数字, 优先级, 和赋值运算符 + +1. **[做一些计算](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/01-数字/练习/01-do-some-calculations)** + +2. **[修复浮点数](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/01-数字/练习/02-fix-the-float)** + +3. **[优先级](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/01-数字/练习/03-precedence)** + +4. **[自增自减运算符](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/01-数字/练习/04-incdecs)** + +5. **[计数器操作](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/01-数字/练习/05-manipulate-a-counter)** + +6. **[简化赋值运算符](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/01-数字/练习/06-simplify-the-assignments)** + +7. **[圆面积](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/01-数字/练习/07-circle-area)** + +8. **[球体面积](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/01-数字/练习/08-sphere-area)** + +9. **[球体体积](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/01-数字/练习/09-sphere-volume)** diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\351\227\256\351\242\230/01-arithmetic-operators.md" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\351\227\256\351\242\230/01-arithmetic-operators.md" new file mode 100644 index 000000000..02b64a810 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\351\227\256\351\242\230/01-arithmetic-operators.md" @@ -0,0 +1,106 @@ +## 下面哪一组运算符是算术运算符? +1. **, /, ^, !, ++, -- +2. *, /, %, +, - *CORRECT* +3. &, |, +, -, / + + +## 下面哪个值可以用余数运算符? +1. 3.54 +2. true +3. 57 *CORRECT* +4. "Try Me!" + +> **4:** 不错的尝试. 但是, 这是不对的. 对不起. +> +> **3:** 这是对的. 余数运算符只对整数起作用. +> + + +## 这个表达式的结果是什么? +```go +8 % 3 +``` +1. 4 +2. 2 *CORRECT* +3. 0 +4. 1 + +## 这个表达式的结果是什么? +```go +-(3 * -2) +``` +1. -6 +2. -1 +3. 0 +4. 6 *CORRECT* + + +## 这个表达式的结果是什么? +```go +var degree float64 = 10 / 4 +``` +1. 2.5 +2. 2.49 +3. 2 *CORRECT* +4. 0 + +> **3:** 这是对的, 一个整数值不能包含小数部分. +> + + +## 这个表达式的结果是什么? +```go +var degree float64 = 3. / 2 +``` +1. 1.5 *CORRECT* +2. 1.49 +3. 1 +4. 0 + +> **1:** 这是对的t. `3.` 使整个表达式成为一个浮点数. +> + + +## `x` 变量的类型是什么? +```go +x := 5 * 2. +``` +1. int +2. float64 *CORRECT* +3. bool +4. string + +> **1:** 仔细看 2 那里. +> +> **2:** 为什么? 因为, `2.` 使表达式成为一个浮点数. Cool. +> +> **3:** oh, 拜托! 生活中并不总是 true 和 false. +> +> **4:** 我看不到任何双引号或反引号, 你能看到吗? +> + + +## `x` 变量的类型是什么? +```go +x := 5 * -(2) +``` +1. int *CORRECT* +2. float64 +3. bool +4. string + +> **1:** 为什么? 因为, 那里只有整数. +> +> **2:** 我看不到那里有小数部分,你能看到吗? +> +> **3:** oh, 拜托! 生活中并不总是 true 和 false. +> +> **4:** 我看不到任何双引号或反引号, 你能看到吗? +> + + +## 哪种数值会导致计算不准确? +1. integers +2. floats *CORRECT* +3. bools +4. strings diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\351\227\256\351\242\230/02-precedence.md" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\351\227\256\351\242\230/02-precedence.md" new file mode 100644 index 000000000..5687cc407 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\351\227\256\351\242\230/02-precedence.md" @@ -0,0 +1,47 @@ +## 这个表达式的结果是什么? +```go +5 - 2 * 5 + 7 +``` +1. 2 *CORRECT* +2. 22 +3. -19 +4. 36 +5. -12 + + +## 这个表达式的结果是什么? +```go +5 - (2 * 5) + 7 +``` +1. 2 *CORRECT* +2. 22 +3. -19 +4. 36 +5. -12 + +> **1:** 该表达式也可以是: 5 + (2 * 5) + 7 + +## 这个表达式的结果是什么? +```go +5 - 2 * (5 + 7) +``` +1. 2 +2. 22 +3. -19 *CORRECT* +4. 36 +5. -12 + + +## 这个表达式的结果是什么? +```go +5. -(2 * 5 + 7) +``` +1. 2 +2. 22 +3. -19 +4. -12 +5. -12.0 *CORRECT* + +> **4:** 你已经很接近了, 但请记住! 有浮点数和整数的表达式的结果总是浮点数. +> +> diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\351\227\256\351\242\230/03-assignment-operations.md" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\351\227\256\351\242\230/03-assignment-operations.md" new file mode 100644 index 000000000..76486147d --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/01-\346\225\260\345\255\227/\351\227\256\351\242\230/03-assignment-operations.md" @@ -0,0 +1,119 @@ +## 哪个表达式使 `n` 增加 1? +```go +var n float64 +``` +1. `n = +1` +2. `n = n++` +3. `n = n + 1` *CORRECT* +4. `++n` + +> **1:** 这只是把 1 分配给了 n. +> +> **2:** 自增自减运算语句不能作为运算符使用. +> +> **4:** Go 不支持前置自增自减运算符号. +> + + +## 哪个表达式使 `n` 减少 1? +```go +var n int +``` +1. `n = -1` +2. `n = n--` +3. `n = n - 1` *CORRECT* +4. `--n` + +> **1:** 这只是把 -1 分配给了 n. +> +> **2:** 自增自减运算语句不能作为运算符使用. +> +> **4:** Go 不支持前置自增自减运算符号. +> + + +## 下面哪段代码等于 `n = n + 1`? +1. `n++` *CORRECT* +2. `n = n++` +3. `++n` +4. `n = n ++ 1` + +> **2:** 自增自减运算语句不能作为运算符使用. +> +> **3:** Go 不支持前置自增自减运算符号. +> +> **4:** 那是什么? ++? +> + + +## 下面哪段代码等于 `n = n + 1`? +1. `n = n++` +2. `n += 1` *CORRECT* +3. `++n` +4. `n = n ++ 1` + +> **1:** 自增自减运算语句不能作为运算符使用. +> +> **3:** Go 不支持前置自增自减运算符号. +> +> **4:** 那是什么? ++? +> + + +## 下面哪段代码等于 `n -= 1`? +1. `n = n--` +2. `n += 1--` +3. `n--` *CORRECT* +4. `--n` + +> **1:** 自增自减运算语句不能作为运算符使用. +> +> **2:** 自增自减运算语句不能作为运算符使用. 还有, 你不能和 `1--` 一起使用它. 该值应该是可寻址的. 你很快就会知道这意味着什么. +> +> **4:** Go 不支持前置自增自减运算符号. +> + + +## Which code below divides the `length` by 10? +1. `length = length // 10` +2. `length /= 10` *CORRECT* +3. `length //= 10` + +> **1:** 那是什么? `//`? +> +> **2:** 这是对的. 这等同于: `length = length / 10` +> +> **3:** 那是什么? `//=`? +> + + +## 下面哪段代码等于 `x = x % 2`? +1. `x = x / 2` +2. `x =% 2` +3. `x %= 2` *CORRECT* + +> **1:** 这是一个除法. 你需要使用余数运算符. +> +> **2:** 很接近... 但是, `%` 在赋值运算符错误的一侧. +> + + +## 下面哪个函数能把字符串值转换成浮点数? +1. `fmtconv.ToFloat` +2. `conv.ParseFloat` +3. `strconv.ParseFloat` *CORRECT* +4. `strconv.ToFloat` + + +## 哪个代码是正确的? +如果你不记得了, 这是函数签名: +```go +func ParseFloat(s string, bitSize int) (float64, error) +``` +1. `strconv.ParseFloat("10", 128)` +2. `strconv.ParseFloat("10", 64)` *CORRECT* +3. `strconv.ParseFloat("10", "64")` +4. `strconv.ParseFloat(10, 64)` + +> **1:** Go 中没有 128-bit 的浮点数 (实际上有,但只在编译时(complie-time)) +> diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/01-raw-string-literal/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/01-raw-string-literal/main.go" new file mode 100644 index 000000000..b7d0898c9 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/01-raw-string-literal/main.go" @@ -0,0 +1,38 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + // The type of a string and a raw string literal + // is the same. They both are strings. + // + // So, they both can be used as a string value. + var s string + s = "how are you?" + s = `how are you?` + fmt.Println(s) + + // string literal + s = "\n\t\"Hello\"\n" + fmt.Println(s) + + // raw string literal + s = ` + + "Hello" +` + + fmt.Println(s) + + // windows path + fmt.Println("c:\\my\\dir\\file") // string literal + fmt.Println(`c:\my\dir\file`) // raw string literal +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/02-concatenation/01/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/02-concatenation/01/main.go" new file mode 100644 index 000000000..b2376e0cc --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/02-concatenation/01/main.go" @@ -0,0 +1,17 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + name, last := "carl", "sagan" + + fmt.Println(name + " " + last) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/02-concatenation/02-assignment-operation/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/02-concatenation/02-assignment-operation/main.go" new file mode 100644 index 000000000..40d9da406 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/02-concatenation/02-assignment-operation/main.go" @@ -0,0 +1,23 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + name, last := "carl", "sagan" + + // assignment operation using string concat + name += " edward" + + // equals to this: + // name = name + " edward" + + fmt.Println(name + " " + last) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/02-concatenation/03-concat-non-strings/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/02-concatenation/03-concat-non-strings/main.go" new file mode 100644 index 000000000..4b70cb702 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/02-concatenation/03-concat-non-strings/main.go" @@ -0,0 +1,52 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "strconv" +) + +func main() { + fmt.Println( + "hello" + ", " + "how" + " " + "are" + " " + "today?", + ) + + // you can combine raw string and string literals + fmt.Println( + `hello` + `, ` + `how` + ` ` + `are` + ` ` + "today?", + ) + + // ------------------------------------------ + // Converting non-string values into string + // ------------------------------------------ + + eq := "1 + 2 = " + sum := 1 + 2 + + // invalid op + // string concat op can only be used with strings + // fmt.Println(eq + sum) + + // you need to convert it using strconv.Itoa + // Itoa = Integer to ASCII + + fmt.Println(eq + strconv.Itoa(sum)) + + // + + // invalid op + // eq = true + " " + false + + eq = strconv.FormatBool(true) + + " " + + strconv.FormatBool(false) + + fmt.Println(eq) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/03-string-length/01-len/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/03-string-length/01-len/main.go" new file mode 100644 index 000000000..b2cfe135e --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/03-string-length/01-len/main.go" @@ -0,0 +1,19 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + name := "carl" + + // strings are made up of bytes + // len function counts the bytes in a string value + fmt.Println(len(name)) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/03-string-length/02-unicode-len/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/03-string-length/02-unicode-len/main.go" new file mode 100644 index 000000000..cf680a6c0 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/03-string-length/02-unicode-len/main.go" @@ -0,0 +1,39 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "unicode/utf8" +) + +func main() { + // strings are made up of bytes + + // len function counts the bytes in a string value. + // + // This string literal contains unicode characters. + // + // And, unicode characters can be 1-4 bytes. + // So, "İnanç" is 7 bytes long, not 5. + // + // İ = 2 bytes + // n = 1 byte + // a = 1 byte + // n = 1 byte + // ç = 2 bytes + // TOTAL = 7 bytes + name := "İnanç" + fmt.Printf("%q is %d bytes\n", name, len(name)) + + // To get the actual characters (or runes) inside + // a utf-8 encoded string value, you should do this: + fmt.Printf("%q is %d characters\n", + name, utf8.RuneCountInString(name)) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/04-project-banger/exercise-solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/04-project-banger/exercise-solution/main.go" new file mode 100644 index 000000000..e14a9f18b --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/04-project-banger/exercise-solution/main.go" @@ -0,0 +1,34 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "os" + "strings" +) + +// NOTE: You should always pass it at least one argument + +func main() { + msg := os.Args[1] + + // it's important to calculate things only once + // so, do not call the repeat function twice + // calling it once is enough + marks := strings.Repeat("!", len(msg)) + s := marks + msg + marks + s = strings.ToUpper(s) + + // you can also type this program more concisely + // like this: + // + // msg := strings.ToUpper(os.Args[1]) + // marks := strings.Repeat("!", len(msg)) + // fmt.Println(marks + msg + marks) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/04-project-banger/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/04-project-banger/main.go" new file mode 100644 index 000000000..4d953160d --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/04-project-banger/main.go" @@ -0,0 +1,27 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "os" + "strings" +) + +// NOTE: You should always pass it at least one argument + +func main() { + msg := os.Args[1] + l := len(msg) + + s := msg + strings.Repeat("!", l) + s = strings.ToUpper(s) + + fmt.Println(s) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/README.md" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/README.md" new file mode 100644 index 000000000..6c5930576 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/README.md" @@ -0,0 +1,18 @@ +# Basic Strings + +1. **[Windows Path](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/02-strings/练习/01-windows-path)** + +2. **[Print JSON](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/02-strings/练习/02-print-json)** + +3. **[Raw Concat](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/02-strings/练习/03-raw-concat)** + +4. **[Count the Chars](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/02-strings/练习/04-count-the-chars)** + +5. **[Improved Banger](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/02-strings/练习/05-improved-banger)** + +6. **[ToLowercase](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/02-strings/练习/06-tolowercase)** + +7. **[Trim It](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/02-strings/练习/07-trim-it)** + +8. **[Right Trim It](https://github.com/inancgumus/learngo/tree/master/08-数字和字符串/02-strings/练习/08-right-trim-it)** + diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/01-windows-path/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/01-windows-path/main.go" new file mode 100644 index 000000000..bc0774c1f --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/01-windows-path/main.go" @@ -0,0 +1,36 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +// --------------------------------------------------------- +// 练习: Windows Path +// +// 1. Change the following program +// 2. It should use a raw string literal instead +// +// HINT +// Run this program first to see its output. +// Then you can easily understand what it does. +// +// 预期输出 +// Your solution should output the same as this program. +// Only that it should use a raw string literal instead. +// --------------------------------------------------------- + +func main() { + // HINTS: + // \\ equals to backslash character + // \n equals to newline character + + path := "c:\\program files\\duper super\\fun.txt\n" + + "c:\\program files\\really\\funny.png" + fmt.Println(path) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/01-windows-path/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/01-windows-path/solution/main.go" new file mode 100644 index 000000000..fa839fbe1 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/01-windows-path/solution/main.go" @@ -0,0 +1,23 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + // this one uses a raw string literal + + // can you see how readable it is? + // compared to the previous one? + + path := `c:\program files\duper super\fun.txt +c:\program files\really\funny.png` + + fmt.Println(path) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/02-print-json/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/02-print-json/main.go" new file mode 100644 index 000000000..2f71c54fe --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/02-print-json/main.go" @@ -0,0 +1,44 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +// --------------------------------------------------------- +// 练习: Print JSON +// +// 1. Change the following program +// 2. It should use a raw string literal instead +// +// HINT +// Run this program first to see its output. +// Then you can easily understand what it does. +// +// 预期输出 +// Your solution should output the same as this program. +// Only that it should use a raw string literal instead. +// --------------------------------------------------------- + +func main() { + // HINTS: + // \t equals to TAB character + // \n equals to newline character + // \" equals to double-quotes character + + json := "\n" + + "{\n" + + "\t\"Items\": [{\n" + + "\t\t\"Item\": {\n" + + "\t\t\t\"name\": \"Teddy Bear\"\n" + + "\t\t}\n" + + "\t}]\n" + + "}\n" + + fmt.Println(json) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/02-print-json/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/02-print-json/solution/main.go" new file mode 100644 index 000000000..aa603e9af --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/02-print-json/solution/main.go" @@ -0,0 +1,29 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +func main() { + // this one uses a raw string literal + + // can you see how readable it is? + // compared to the previous one? + + json := ` +{ + "Items": [{ + "Item": { + "name": "Teddy Bear" + } + }] +}` + + fmt.Println(json) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/03-raw-concat/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/03-raw-concat/main.go" new file mode 100644 index 000000000..21026c375 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/03-raw-concat/main.go" @@ -0,0 +1,46 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import "fmt" + +// --------------------------------------------------------- +// 练习: Raw Concat +// +// 1. Initialize the name variable +// by getting input from the command line +// +// 2. Use concatenation operator to concatenate it +// with the raw string literal below +// +// NOTE +// You should concatenate the name variable in the correct +// place. +// +// 预期输出 +// Let's say that you run the program like this: +// go run main.go inanç +// +// Then it should output this: +// hi inanç! +// how are you? +// --------------------------------------------------------- + +func main() { + // uncomment the code below + // name := "and get the name from the command-line" + + // replace and concatenate the `name` variable + // after `hi ` below + + msg := `hi CONCATENATE-NAME-VARIABLE-HERE! +how are you?` + + fmt.Println(msg) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/03-raw-concat/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/03-raw-concat/solution/main.go" new file mode 100644 index 000000000..945659d91 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/03-raw-concat/solution/main.go" @@ -0,0 +1,23 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "os" +) + +func main() { + name := os.Args[1] + + msg := `hi ` + name + `! +how are you?` + + fmt.Println(msg) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/04-count-the-chars/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/04-count-the-chars/main.go" new file mode 100644 index 000000000..91d134c6c --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/04-count-the-chars/main.go" @@ -0,0 +1,38 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "os" +) + +// --------------------------------------------------------- +// 练习: Count the Chars +// +// 1. Change the following program to work with unicode +// characters. +// +// INPUT +// "İNANÇ" +// +// 预期输出 +// 5 +// --------------------------------------------------------- + +func main() { + // Currently it returns 7 + // Because, it counts the bytes... + // It should count the runes (codepoints) instead. + // + // When you run it with "İNANÇ", it should return 5 not 7. + + length := len(os.Args[1]) + fmt.Println(length) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/04-count-the-chars/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/04-count-the-chars/solution/main.go" new file mode 100644 index 000000000..f96b30fd6 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/04-count-the-chars/solution/main.go" @@ -0,0 +1,20 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "os" + "unicode/utf8" +) + +func main() { + length := utf8.RuneCountInString(os.Args[1]) + fmt.Println(length) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/05-improved-banger/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/05-improved-banger/main.go" new file mode 100644 index 000000000..12375f43a --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/05-improved-banger/main.go" @@ -0,0 +1,36 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "os" + "strings" +) + +// --------------------------------------------------------- +// 练习: Improved Banger +// +// Change the Banger program the work with Unicode +// characters. +// +// INPUT +// "İNANÇ" +// +// 预期输出 +// İNANÇ!!!!! +// --------------------------------------------------------- + +func main() { + msg := os.Args[1] + + s := msg + strings.Repeat("!", len(msg)) + + fmt.Println(s) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/05-improved-banger/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/05-improved-banger/solution/main.go" new file mode 100644 index 000000000..305df840c --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/05-improved-banger/solution/main.go" @@ -0,0 +1,25 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "os" + "strings" + "unicode/utf8" +) + +func main() { + msg := os.Args[1] + l := utf8.RuneCountInString(msg) + + s := msg + strings.Repeat("!", l) + + fmt.Println(s) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/06-tolowercase/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/06-tolowercase/main.go" new file mode 100644 index 000000000..1c2fe0a23 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/06-tolowercase/main.go" @@ -0,0 +1,31 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +// --------------------------------------------------------- +// 练习: ToLowercase +// +// 1. Look at the documentation of strings package +// 2. Find a function that changes the letters into lowercase +// 3. Get a value from the command-line +// 4. Print the given value in lowercase letters +// +// HINT +// Check out the strings package from Go online documentation. +// You will find the lowercase function there. +// +// INPUT +// "SHEPARD" +// +// 预期输出 +// shepard +// --------------------------------------------------------- + +func main() { +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/06-tolowercase/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/06-tolowercase/solution/main.go" new file mode 100644 index 000000000..aee60d807 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/06-tolowercase/solution/main.go" @@ -0,0 +1,19 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "os" + "strings" +) + +func main() { + fmt.Println(strings.ToLower(os.Args[1])) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/07-trim-it/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/07-trim-it/main.go" new file mode 100644 index 000000000..6f90fa7a0 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/07-trim-it/main.go" @@ -0,0 +1,39 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" +) + +// --------------------------------------------------------- +// 练习: Trim It +// +// 1. Look at the documentation of strings package +// 2. Find a function that trims the spaces from +// the given string +// 3. Trim the text variable and print it +// +// 预期输出 +// The weather looks good. +// I should go and play. +// --------------------------------------------------------- + +func main() { + msg := ` + + The weather looks good. +I should go and play. + + + + ` + + fmt.Println(msg) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/07-trim-it/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/07-trim-it/solution/main.go" new file mode 100644 index 000000000..a57b88ca9 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/07-trim-it/solution/main.go" @@ -0,0 +1,27 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "strings" +) + +func main() { + msg := ` + + The weather looks good. +I should go and play. + + + + ` + + fmt.Println(strings.TrimSpace(msg)) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/08-right-trim-it/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/08-right-trim-it/main.go" new file mode 100644 index 000000000..4c3ada69c --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/08-right-trim-it/main.go" @@ -0,0 +1,37 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" +) + +// --------------------------------------------------------- +// 练习: Right Trim It +// +// 1. Look at the documentation of strings package +// 2. Find a function that trims the spaces from +// only the right-most part of the given string +// 3. Trim it from the right part only +// 4. Print the number of characters it contains. +// +// 限制 +// Your program should work with unicode string values. +// +// 预期输出 +// 5 +// --------------------------------------------------------- + +func main() { + // currently it prints 17 + // it should print 5 + + name := "inanç " + fmt.Println(len(name)) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/08-right-trim-it/solution/main.go" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/08-right-trim-it/solution/main.go" new file mode 100644 index 000000000..4a2690757 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/exercises/08-right-trim-it/solution/main.go" @@ -0,0 +1,24 @@ +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// +// For more tutorials : https://learngoprogramming.com +// In-person training : https://www.linkedin.com/in/inancgumus/ +// Follow me on twitter: https://twitter.com/inancgumus + +package main + +import ( + "fmt" + "strings" + "unicode/utf8" +) + +func main() { + name := "inanç " + + name = strings.TrimRight(name, " ") + l := utf8.RuneCountInString(name) + + fmt.Println(l) +} diff --git "a/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/questions/questions.md" "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/questions/questions.md" new file mode 100644 index 000000000..cedd71837 --- /dev/null +++ "b/translation/chinese/08-\346\225\260\345\255\227\345\222\214\345\255\227\347\254\246\344\270\262/02-\345\255\227\347\254\246\344\270\262/questions/questions.md" @@ -0,0 +1,222 @@ +## 这个表达式的结果是什么? +```go +"\"Hello\\"" + ` \"World\"` +``` + +1. "Hello" "World" +2. "Hello" \"World\" *CORRECT* +3. "Hello" `"World"` +4. "\"Hello\" `\"World\"`" + +> **1:** Go doesn't interpret the escape sequences in raw string literals. +> +> **2:** That's right. Go interprets `\"` as `"` but it doesn't do so for ` \"World\"`. +> + + +## What's the best way to represent the following text in the code? +```xml + + + "Teddy Bear" + + +``` + +1. *CORRECT* +```go +` + + "Teddy Bear" + +` +``` + +2. +```go +" + + "Teddy Bear" + +" +``` + +3. +```go +" + + \"Teddy Bear\" + +" +``` + +4. +```go +` + + \"Teddy Bear\" + +` +``` + +> **2-3:** You can't write a string literal like that. It can't be multiple-lines. +> +> **4:** You don't need to use escape sequences inside raw string literals. +> + + +## What's the result of the following expression? +```go +len("lovely") +``` + +1. 7 +2. 8 +3. 6 *CORRECT* +4. 0 + +> **2:** Remember! "a" is 1 char. `a` is also 1 char. +> + + +## What's the result of the following expression? +```go +len("very") + len(`\"cool\"`) +``` + +1. 8 +2. 12 *CORRECT* +3. 16 +4. 10 + +> **1:** There are also double-quotes, count them as well. +> +> **2:** That's right. Go doesn't interpreted \" in raw string literals. +> +> **3:** Remember! "very" is 4 characters. `very` is also 4 characters. +> +> **4:** Remember! Go doesn't interpreted \" in raw string literals. +> + + +## What's the result of the following expression? +```go +len("very") + len("\"cool\"") +``` + +1. 8 +2. 12 +3. 16 +4. 10 *CORRECT* + +> **1:** There are also double-quotes, count them as well. +> +> **2:** Remember! Go interprets escape sequences in string literals. +> +> **4:** That's right. Go does interpret \" in a string literal. So, "\"" means ", which is 1 character. +> + + +## What's the result of the following expression? +```go +len("péripatéticien") +``` + +**HINT:** é is 2 bytes long. And, the len function counts the bytes not the letters. + +**USELESS INFORMATION:** "péripatéticien" means "wanderer". + +1. 14 +2. 16 *CORRECT* +3. 18 +4. 20 + +> **1:** Remember! é is 2 bytes long. +> +> **2:** An english letter is 1 byte long. However, é is 2 bytes long. So, that makes up 16 bytes. Cool. +> +> **3:** You didn't count the double-quotes, did you? +> + + +## How can you find the correct length of the characters in this string literal? +```go +"péripatéticien" +``` + +1. `len(péripatéticien)` +2. `len("péripatéticien")` +3. `utf8.RuneCountInString("péripatéticien")` *CORRECT* +4. `unicode/utf8.RuneCountInString("péripatéticien")` + +> **1:** Where are the double-quotes? +> +> **2:** This only finds the bytes in a string value. +> +> **4:** You're close. But, the package's name is utf8 not unicode/utf8. +> + + +## What's the result of the following expression? +```go +utf8.RuneCountInString("péripatéticien") +``` + +1. 16 +2. 14 *CORRECT* +3. 18 +4. 20 + +> **1:** This is its byte count. `RuneCountInString` counts the runes (codepoints) not the bytes. +> +> **2:** That's right. `RuneCountInString` returns the number of runes (codepoints) in a string value. +> + + +## Which package contains string manipulation functions? +1. string +2. unicode/utf8 +3. strings *CORRECT* +4. unicode/strings + + +## 这个表达式的结果是什么? +```go +strings.Repeat("*x", 3) + "*" +``` + +**HINT:** Repeat function repeats the given string. + +1. `*x*x*x` +2. `x*x*x` +3. `*x3` +4. `*x*x*x*` *CORRECT* + +> **1:** You're close but you missed the concatenation at the end. +> +> **2:** Look closely. +> +> **3:** Wow! You should really watch the lectures again. Sorry. +> +> **4:** That's right. Repeat function repeats the given string. And, the concatenation operator combines the strings. +> + + +## 这个表达式的结果是什么? +```go +strings.ToUpper("bye bye ") + "see you!" +``` + +1. `bye bye see you!` +2. `BYE BYE SEE YOU!` +3. `bye bye + see you!` +4. `BYE BYE see you!` *CORRECT* + +> **1:** You missed the ToUpper? +> +> **2:** You're close but look closely. ToUpper only changes the first part of the string there. +> +> **3:** Not even close. Sorry. +> +> **4:** Perfect! Good catch! ToUpper only changes the first part of the string there. +>