はい、できますが、この場合は不要です。
syntax
関数は、コードが実行され出力が生成されるように、コード内の構文やその他のエラーを修正します。 idiom
関数は、コードをより慣用的な形で書き換えます。
package main
import (
"fmt"
)
func syntax() {
x := []int{
48, 96, 86, 68,
57, 82, 63, 70,
37, 34, 83, 27,
19, 97, 9, 17,
}
for a, b := 0, 1; a < len(x) && b < len(x); a, b = a+1, b+1 {
if x[a] > x[b] {
x = append(x[:1], x[1+1:]...)
fmt.Println("x[1+1:]x)", x)
} else {
x = append(x[:0], x[0+1:]...)
fmt.Println("x[0+1:]x)", x)
}
}
}
func idiom() {
x := []int{
48, 96, 86, 68,
57, 82, 63, 70,
37, 34, 83, 27,
19, 97, 9, 17,
}
for i := 1; i < len(x); i++ {
if x[i-1] > x[i] {
x = append(x[:1], x[1+1:]...)
fmt.Println("x[1+1:]x)", x)
} else {
x = append(x[:0], x[0+1:]...)
fmt.Println("x[0+1:]x)", x)
}
}
}
func main() {
syntax()
fmt.Println()
idiom()
}
出力:
$ go run beginner.go
x[0+1:]x) [96 86 68 57 82 63 70 37 34 83 27 19 97 9 17]
x[1+1:]x) [96 68 57 82 63 70 37 34 83 27 19 97 9 17]
x[0+1:]x) [68 57 82 63 70 37 34 83 27 19 97 9 17]
x[0+1:]x) [57 82 63 70 37 34 83 27 19 97 9 17]
x[1+1:]x) [57 63 70 37 34 83 27 19 97 9 17]
x[1+1:]x) [57 70 37 34 83 27 19 97 9 17]
x[0+1:]x) [70 37 34 83 27 19 97 9 17]
x[0+1:]x) [37 34 83 27 19 97 9 17]
x[0+1:]x) [96 86 68 57 82 63 70 37 34 83 27 19 97 9 17]
x[1+1:]x) [96 68 57 82 63 70 37 34 83 27 19 97 9 17]
x[0+1:]x) [68 57 82 63 70 37 34 83 27 19 97 9 17]
x[0+1:]x) [57 82 63 70 37 34 83 27 19 97 9 17]
x[1+1:]x) [57 63 70 37 34 83 27 19 97 9 17]
x[1+1:]x) [57 70 37 34 83 27 19 97 9 17]
x[0+1:]x) [70 37 34 83 27 19 97 9 17]
x[0+1:]x) [37 34 83 27 19 97 9 17]
はA Tour of Goを取るためにリンクをクリックしてください。
Golang forループ:https://golang.org/doc/effective_go.html#for – jrahhali
はいできます。構文が間違っています(https://golang.org/doc/effective_go.html#for –