package product type ItemStatus int8 func (p *ItemStatus) ToString() string { s, _ := StatusToString(*p) return s } const ( StatusActive ItemStatus = 1 + iota // 上架 StatusInactive // 下架 StatusOutOfStock // 缺貨 StatusUnderReview // 商品審核中 ) const ( StatusActiveStr = "active" StatusInactiveStr = "inactive" StatusOutOfStockStr = "out_of_stock" StatusUnderReviewStr = "under_review" ) var statusToStringMap = map[ItemStatus]string{ StatusActive: StatusActiveStr, StatusInactive: StatusInactiveStr, StatusOutOfStock: StatusOutOfStockStr, } var stringToStatusMap = map[string]ItemStatus{ StatusActiveStr: StatusActive, StatusInactiveStr: StatusInactive, StatusOutOfStockStr: StatusOutOfStock, StatusUnderReviewStr: StatusUnderReview, } // StatusToString 將 ProductItemStatus 轉換為字串 func StatusToString(status ItemStatus) (string, bool) { str, ok := statusToStringMap[status] return str, ok } // StringToItemStatus 將字串轉換為 ProductItemStatus func StringToItemStatus(statusStr string) (ItemStatus, bool) { status, ok := stringToStatusMap[statusStr] return status, ok }