Using a pointer to array

后端 未结 5 1714
一向
一向 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:07

    The length is part of the array's type, you can get length of an array by the len() built-in function. So you needn't pass the xlen, hlen arguments.

    In Go, you can almost always use slice when passing array to a function. In this case, you don't need pointers. Actually, you need not pass the y argument. It's the C's way to output array.

    In Go style:

    func conv(x, h []int) []int {
        y := make([]int, len(x)+len(h))
        for i, v := range x { 
            for j, u := range h { 
                y[i+j] = v * u 
            }   
        }   
        return y
    }
    

    Call the function:

    conv(x[0:], h[0:])
    

提交回复
热议问题