Monday, June 23, 2025
HomeGolangSuggestions on avoiding duplicate textual content lookup values at compile time -...

Suggestions on avoiding duplicate textual content lookup values at compile time – Code Overview


I need to have lookup strings which might be distinctive at compile time for the native package deal.

I’ve the next :

sort Key int

const (
	txt Key = iota + 1
	greeting
	// cannot add txt once more right here - **compile** will fail
	newKey // not but used
)

var id map[Key]string

func init() {
	id = map[Key]string{txt: "txt", greeting: "greeting"}
	// cannot add txt: "something" since **compile** will fail
	// may add newKey: "txt" - however this requires **deliberate foolishness**

	library.NewBlockType("server.log", "misc", setup).Add(
		textual content.New("Log "),
		stringinput.New(id[greeting]).Empty("greeting"),
		// cannot cease reuse of id incorrectly, e.g. by :
		// stringinput.New(id[greeting]).Empty("duplicate greeting"),
		// nevertheless the generated UI is seen so this needs to be very apparent
		textual content.New(" "),
		stringinput.New(id[txt]).Empty("message"))
}

Right here Secret is used to restrict string lookup on the const values solely

This presents some (however not all) detection of copy/paste errors on compilation.

Any ideas/strategies please :slight_smile:

I’m additionally if that is idiomatic code/sample or seen/used elsewhere…

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments