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

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


I need to have lookup strings which are distinctive at compile time for the native bundle.

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
	// might 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"),
		// nonetheless the generated UI is seen so this ought to be very apparent
		textual content.New(" "),
		stringinput.New(id[txt]).Empty("message"))
}

Right here Key’s used to restrict string lookup on the const values solely

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

Any ideas/options please :slight_smile:

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

Good day there. Why to not use stringer?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments