Using a pointer to array

后端 未结 5 1708
一向
一向 2020-12-05 06:39

I\'m having a little play with google\'s Go language, and I\'ve run into something which is fairly basic in C but doesn\'t seem to be covered in the documentation I\'ve seen

5条回答
  •  爱一瞬间的悲伤
    2020-12-05 07:26

    Here's a working Go program.

    package main
    
    import "fmt"
    
    func conv(x, h []int) []int {
        y := make([]int, len(x)+len(h)-1)
        for i := 0; i < len(x); i++ {
            for j := 0; j < len(h); j++ {
                y[i+j] += x[i] * h[j]
            }
        }
        return y
    }
    
    func main() {
        x := []int{1, 2}
        h := []int{7, 8, 9}
        y := conv(x, h)
        fmt.Println(len(y), y)
    }
    

    To avoid wrong guesses, read the Go documentation: The Go Programming Language.

提交回复
热议问题