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: connect.go
    path of file: ./repos/kubeedge/vendor/github.com/256dpi/gomqtt/packet
    
    the code of the file until where you have to start completion: package packet

import (
	"bytes"
	"encoding/binary"
	"fmt"
)

// The supported MQTT versions.
const (
	Version311 byte = 4
	Version31  byte = 3
)

var version311Name = []byte("MQTT")
var version31Name = []byte("MQIsdp")

// A Connect packet is sent by a client to the server after a network
// connection has been established.
type Connect struct {
	// The clients client id.
	ClientID string

	// The keep alive value.
	KeepAlive uint16

	// The authentication username.
	Username string

	// The authentication password.
	Password string

	// The clean session flag.
	CleanSession bool

	// The will message.
	Will *Message

	// The MQTT version 3 or 4 (defa
    
func (cp *Connect) len() int {
	total := 0

	if cp.Version == Version31 {
		// 2 bytes protocol name length
		// 6 bytes protocol name
		// 1 byte protocol version
		total += 2 + 6 + 1
	} else {
		// 2 bytes protocol name length
		// 4 bytes protocol name
		// 1 byte protocol version
		total += 2 + 4 + 1
	}

	// 1 byte connect flags
	// 2 bytes keep alive timer
	total += 1 + 2

	// add the clientID length
	total += 2 + len(cp.ClientID)

	// add the will topic and will message length
	if cp.Will != nil {
		total += 2 + len(cp.Will.Topic) + 2 + len(cp.Will.Payload)
	}

	// add the username length
	if len(cp.Username) > 0 {
		total += 2 + len(cp.Username)
	}

	// add the password length
	if len(cp.Password) > 0 {
		total += 2 + len(cp.Password)
	}

	return total
}