You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: 116.Populating Next Right Pointers in Each Node.go
    path of file: ./repos/LeetCode-Go/leetcode/0116.Populating-Next-Right-Pointers-in-Each-Node
    
    the code of the file until where you have to start completion: package leetcode

type Node struct {
	Val   int
	Left  *Node
	Right 
    
func connect(root *Node) *Node {
	if root == nil {
		return root
	}
	q := []*Node{root}
	for len(q) > 0 {
		var p []*Node
		// 遍历这一层的所有节点
		for i, node := range q {
			if i+1 < len(q) {
				node.Next = q[i+1]
			}
			if node.Left != nil {
				p = append(p, node.Left)
			}
			if node.Right != nil {
				p = append(p, node.Right)
			}
		}
		q = p
	}
	return root
}