我如何改进代码?

时间:2022-10-11 03:22:54

I'm beginner in programming. Could i use two elements in golang for-loops? If you know answer, or material which i should read, please help me.

我是编程的初学者。我可以在golang for循环中使用两个元素吗?如果您知道答案或我应该阅读的材料,请帮助我。

package main

import (
    "fmt"
)

func main() {
    x := []int{
        48, 96, 86, 68,
        57, 82, 63, 70,
        37, 34, 83, 27,
        19, 97, 9, 17,
    }

    for a := 0, b := 1; a++, b++ {
        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)
        }
    }

}

1 个解决方案

#1


0  

Yes, you can, although it's unnecessary in this case.

是的,你可以,虽然在这种情况下没有必要。

The syntax function fixes the syntax and other errors in your code so that your code runs and produces output. The idiom function rewrites your code in a more idiomatic form.

语法函数修复代码中的语法和其他错误,以便代码运行并生成输出。成语函数以更惯用的形式重写您的代码。

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()
}

Output:

$ 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]

Follow the link to take A Tour of Go.

点击链接进行A Tour of Go。

#1


0  

Yes, you can, although it's unnecessary in this case.

是的,你可以,虽然在这种情况下没有必要。

The syntax function fixes the syntax and other errors in your code so that your code runs and produces output. The idiom function rewrites your code in a more idiomatic form.

语法函数修复代码中的语法和其他错误,以便代码运行并生成输出。成语函数以更惯用的形式重写您的代码。

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()
}

Output:

$ 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]

Follow the link to take A Tour of Go.

点击链接进行A Tour of Go。