A professional VS Code extension to automate the generation of Golang enums using the iota pattern. It handles construction, string conversion, and provides lookup method.
Features
- Generate Enum: Create a full enum structure from scratch. Automatically detects package and determines the type name from the filename.
- Add Cases: Add new cases to an existing enum at any time. It merges existing cases with new ones.
- Remove Cases: Easily remove one or more cases from an existing enum.
Commands
| Command | Description |
|---|---|
Golang Enum Generator: Generate |
Generates a new enum or replaces an existing one with new cases. |
Golang Enum Generator: Add |
Appends new cases to an existing enum. |
Golang Enum Generator: Remove |
Removes specified cases from an existing enum. |
Usage
- Open or create a
.gofile (e.g.,user_role.go). - Open the Command Palette (
Ctrl+Shift+PorCmd+Shift+P). - Search for one of the
Golang Enum Generatorcommands. - Follow the prompts to enter case names (comma-separated).
Example Output
package vo
import "strings"
type DeviceStatus int
const (
DeviceStatusUnknown DeviceStatus = iota
DeviceStatusActivated
DeviceStatusDeactivated
DeviceStatusUnknownValue = "UNKNOWN"
DeviceStatusActivatedValue = "ACTIVATED"
DeviceStatusDeactivatedValue = "DEACTIVATED"
)
func NewDeviceStatusFromString(value string) DeviceStatus {
v := strings.ToUpper(strings.TrimSpace(value))
switch v {
case DeviceStatusUnknownValue:
return DeviceStatusUnknown
case DeviceStatusActivatedValue:
return DeviceStatusActivated
case DeviceStatusDeactivatedValue:
return DeviceStatusDeactivated
default:
return DeviceStatusUnknown
}
}
func (e DeviceStatus) Value() string {
return [...]string{
DeviceStatusUnknownValue,
DeviceStatusActivatedValue,
DeviceStatusDeactivatedValue,
}[e]
}
Requirements
- VS Code version 1.80.0 or higher.
- A Go environment for formatting (recommended).
License
This project is licensed under the MIT License - see the LICENSE file for details.