Skip to content
/ varfmt Public

Convert string into Go variable name following Go naming convention

License

Notifications You must be signed in to change notification settings

achiku/varfmt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

varfmt

Build Status GitHub license Doc Go Report Card

Convert string into Go variable name following Go naming convention

Why created

Although there are a lot of needs to generate go struct from various sources (DDL, yml, toml, JSON, etc), I couldn't find any common library to format struct field names that follow go naming convention. Plus, I found really nice little piece of code that formats variable name in ChimeraCoder/gojson.

How to use

package varfmt_test

import (
	"fmt"

	"github.com/achiku/varfmt"
)

func ExamplePublicVarName() {
	malformattedVars := []string{
		"foo",
		"foo_bar",
		"fooBar",
		"foo_bar_buz",
		"foo_bar___buz",
		"foo_id",
		"foo_tls",
		"foo_json",
	}

	for _, s := range malformattedVars {
		fmt.Printf("%s -> %s\n", s, varfmt.PublicVarName(s))
	}
	// Output:
	// foo -> Foo
	// foo_bar -> FooBar
	// fooBar -> FooBar
	// foo_bar_buz -> FooBarBuz
	// foo_bar___buz -> FooBarBuz
	// foo_id -> FooID
	// foo_tls -> FooTLS
	// foo_json -> FooJSON

}

Credits

Code is almost entirely borrowed from https://github.com/ChimeraCoder/gojson. Huge thanks to ChimeraCoder !!

About

Convert string into Go variable name following Go naming convention

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages