【发布时间】:2021-06-28 05:16:16
【问题描述】:
我正在尝试将结构字段“Category”转换为字符串,以便可以在 ConcatenateNotification 中进行连接。
有人知道怎么做吗?
请在下面查看我的代码 sn-p。
//Category is enum of
//available notification types (semantic meaning of the notification)
type Category string
// Category allowed values
const (
FlowFailure Category = "flow_failure"
WriterResult Category = "writer_result"
)
//Notification is struct containing all information about notification
type Notification struct {
UserID int
Category Category
}
//ConcatenateNotification loads data from Notification struct and concatenates them into one string, "\n" delimited
func ConcatenateNotification(n Notification) (msg string) {
values := []string{}
values = append(values, "UserID: " + strconv.Itoa(n.UserID))
values = append(values, "Category: " + (n.Category)) // Anybody knows how to convert this value to string?
msg = strings.Join(values, "\n")
return msg
【问题讨论】:
-
您可能想使用
fmt.Sprintf("%v", n.Category)。我想它会解决你的问题。 -
@Giannis 太过分了,
Category已经是字符串类型了。
标签: go struct string-conversion