在运行时覆盖Golang JSON标签值

I have the following struct in in my Web Application using "encoding/json"

type CourseAssignment struct {
    Semester int `json:"semester"  xml:"semester"`
    Lecture Lecture `json:"-"  xml:"-"`
    Cos Cos `json:"-"  xml:"-"`
    Links map[string][]Link `json:"links,omitempty" xml:"links,omitempty"`
}

Lecture and Cos are complex structs themselves that i dont want to be included in my serialized json which i indicate by setting json:"-"

This works perfectly.

I want to override that behavior on demand during runtime, how do i do that without writing my own Serialization code ?

Edit: My Own Solution:

func (r *CourseAssignment) Expand(depth int) CourseAssignment {

    if depth <= 0 {
        return *r
    }

    tmp := *r
    tmp.LectureEx = tmp.Lecture
    tmp.CosEx = tmp.Cos
    tmp.Links = nil 
    return tmp
}

type CourseAssignment struct {
    Semester int `json:"semester"  xml:"semester"`
    Lecture *Lecture `json:"-"  xml:"-"`
    Cos *Cos `json:"-"  xml:"-"`
    Links map[string][]Link `json:"links,omitempty" xml:"links,omitempty"`
    LectureEx  *Lecture   `json:"lecture,omitempty"  xml:"lecture,omitempty"`
    CosEx *Cos `json:"course_of_study,omitempty" xml:"course_of_study,omitempty"`   
}

When i want to include the fields i create a Copy of the Object using expand that fills fields that contain the same references but show up in the serialization.

You can read/get struct tag values using StructTag from reflect package:

package main

import (
    "fmt"
    "reflect"
)

type CourseAssignment struct {
    Semester int `json:"semester"  xml:"semester"`
}

func main() {
    ca := CourseAssignment{}
    st := reflect.TypeOf(ca)
    field := st.Field(0)
    fmt.Println(field.Tag.Get("json"))
}

There is no method to change a struct tag field in the standard library.

However, there are open-source libraries that do exactly that, like Retag.

What I've done in similar cases is to set these kinds of fields to omitempty, and then empty them before serializing in the cases where I don't want them included in JSON:

type CourseAssignment struct {
    Semester int `json:"semester"  xml:"semester"`
    Lecture *Lecture `json:"lecture,omitempty"  xml:"-"`
    Cos *Cos `json:"cos,omitempty"  xml:"-"`
    Links map[string][]Link `json:"links,omitempty" xml:"links,omitempty"`
}

// Want to serialize without including Lecture/Cos:
aCourseAssignment.Lecture = nil
aCourseAssignment.Cos = nil
thing,err := json.Marshal(aCourseAssignment)