Documentation ¶
Overview ¶
Package gocase is a package to convert normal CamelCase to Golang's CamelCase and vice versa. Golang's CamelCase means a string that takes into account to Go's common initialisms. For more details, please see initialisms section in Staticcheck.
Example ¶
package main import ( "fmt" "github.com/takuoki/gocase" ) func main() { strs := []string{"IpAddress", "defaultDnsServer", "JsonFile", "CsvFile"} fmt.Println("-- Default converter --") for _, str := range strs { fmt.Printf("%s --To-> %s --Revert-> %s\n", str, gocase.To(str), gocase.Revert(gocase.To(str))) } fmt.Println() fmt.Println("-- Custom converter --") conv1, _ := gocase.New(gocase.WithInitialisms("JSON", "CSV")) for _, str := range strs { fmt.Printf("%s --To-> %s --Revert-> %s\n", str, conv1.To(str), conv1.Revert(conv1.To(str))) } fmt.Println() fmt.Println("-- Custom converter (add to default) --") initialisms := append([]string{"JSON", "CSV"}, gocase.DefaultInitialisms...) conv2, _ := gocase.New(gocase.WithInitialisms(initialisms...)) for _, str := range strs { fmt.Printf("%s --To-> %s --Revert-> %s\n", str, conv2.To(str), conv2.Revert(conv2.To(str))) } }
Output: -- Default converter -- IpAddress --To-> IPAddress --Revert-> IpAddress defaultDnsServer --To-> defaultDNSServer --Revert-> defaultDnsServer JsonFile --To-> JSONFile --Revert-> JsonFile CsvFile --To-> CsvFile --Revert-> CsvFile -- Custom converter -- IpAddress --To-> IpAddress --Revert-> IpAddress defaultDnsServer --To-> defaultDnsServer --Revert-> defaultDnsServer JsonFile --To-> JSONFile --Revert-> JsonFile CsvFile --To-> CSVFile --Revert-> CsvFile -- Custom converter (add to default) -- IpAddress --To-> IPAddress --Revert-> IpAddress defaultDnsServer --To-> defaultDNSServer --Revert-> defaultDnsServer JsonFile --To-> JSONFile --Revert-> JsonFile CsvFile --To-> CSVFile --Revert-> CsvFile
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultInitialisms = []string{
"ACL", "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP",
"HTTPS", "JSON", "QPS", "RAM", "RPC", "SLA", "SMTP", "SQL", "SSH", "TCP",
"TLS", "TTL", "UDP", "GID", "UUID", "URI", "URL", "UTF8", "VM", "XML",
"XMPP", "XSRF", "XSS", "SIP", "RTP", "AMQP", "DB", "TS",
"UID", "ID", "IP", "UI",
}
DefaultInitialisms is a list of default initialisms. This list is based on Staticcheck. For more details, please see initialisms section.
Functions ¶
Types ¶
type Converter ¶ added in v1.1.0
type Converter struct {
// contains filtered or unexported fields
}
Converter represents a conversion object. If you need a conversion other than the default, you need create a new conversion object.
type Option ¶ added in v1.1.0
type Option interface {
// contains filtered or unexported methods
}
Option is an option that configures Converter.
func WithInitialisms ¶ added in v1.1.0
WithInitialisms is an option to set initialisms. If you want to add to the default initialisms, use `DefaultInitialisms`.