审查生成的资料模型

来自 结构体
Go 1.27 高级 6分钟 找出 4处问题

根据任务要求审查这个生成的资料模型。

支持并发修改资料名称,并返回 JSON 响应快照;快照中的 roles 不能修改内部状态,而且绝不能暴露 API key。

Go
type Profile struct {
    mu          sync.Mutex
    ID          string
    DisplayName string
    Roles       []string
    apiKey      string
}

type ProfileView struct {
    ID          string   `json:"id"`
    DisplayName string   `json:"display_name"`
    Roles       []string `json:"roles"`
    APIKey      string   `json:"api_key"`
}

func (p Profile) Rename(name string) {
    p.mu.Lock()
    defer p.mu.Unlock()
    p.DisplayName = name
}

func (p *Profile) Snapshot() ProfileView {
    return ProfileView{p.ID, p.DisplayName, p.Roles, p.apiKey}
}

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误