类型不匹配会在生成JWT令牌时导致错误

I am trying to generate a JWT token but having trouble when it comes to converting everything to a byte array.

func GenerateToken(uid, cid int64) string{
    header := `{"alg": "HS256","typ": "JWT"}`
    header = base64.URLEncoding.EncodeToString([]byte(header))
    var b structs.JwtBody
    b.UID = uid
    b.CID = cid
    body, _ := json.Marshal(b)
    key := []byte(secret)
    h := hmac.New(sha256.New, key)
    h.Write([]byte(header + "." + body))
    signature := base64.URLEncoding.EncodeToString(h.Sum(nil))
    jwt := header + "." + body + "." + signature
    return jwt
}

I am getting an error with this line because the types do not match up.

h.Write([]byte(header + "." + body))

How do I fix this?

Change the h.Write line to the following:

h.Write([]byte(header))
h.Write([]byte("."))
h.Write(body)

Splitting the the hash calculation into three separate calls should be (slightly) faster than concatenating the pieces together and writing that.