Skip to content
This repository has been archived by the owner on May 9, 2021. It is now read-only.

Question: don't use leading k in Go names. Why? #258

Closed
DavidGamba opened this issue Nov 14, 2016 · 9 comments
Closed

Question: don't use leading k in Go names. Why? #258

DavidGamba opened this issue Nov 14, 2016 · 9 comments

Comments

@DavidGamba
Copy link

I can't find any reference indicating why Golint warns when doing this. Some explanation would be greatly appreciated!

@dominikh
Copy link
Member

Idiomatic Go doesn't use Hungarian notation and variable names shouldn't be prefixed with their intended use. For example, a constant should be named foo or Foo, not kFoo or KFoo.

@DavidGamba
Copy link
Author

Thank you for the answer. I have a KBToMB function and it drives me crazy to see the lint error not knowing why.

@dominikh
Copy link
Member

@DavidGamba Are you using a recent version of golint? Looking at the code, it only matches on lowercase k, not uppercase K. It also doesn't match if the letter following the k isn't uppercase. So neither KBToMB nor kbToMB should get flagged.

@DavidGamba
Copy link
Author

@dominikh, I apologize, the actual name I have is kBToMB because it is unexported. I should be using kbToMB to avoid mixing case in initialisms.

@agnivade
Copy link

Hi,

I am bit by this rule. I have a kafka consumer and a partition consumer in my struct. I am naming them as kConsumer and pConsumer. But the kConsumer is throwing lint error. I would really want to avoid naming it consumer just to make it clear that its a kafka consumer.

What is the recommendation here ? Do I have to name it kafkaConsumer ?

@joelpresence
Copy link

I am also having issues with this where I am parsing an array of strings of the form []string{"key1", "value1","key2","value2"} for example:

var arr = []string{"key1", "value1","key2","value2"}
for i, kOrV := range arr {
}

I am clearly NOT using Hungarian notation here - the kOrV means keyOrValue so why am I being falsely penalized by golint here?

This is clearly a bug in golint in this case, or at best an annoying false positive. Is there any way we can fix this?

Thanks!

@nilsanderselde
Copy link

I'm writing a web server for a site which serves information about a phonetic orthography I've made for English for fun. In one template processing file I have these vars:

	ɑIPA  = "IPA: ɑ/ɔ/ɒ"
	æIPA  = "IPA: æ"
	ɛIPA  = "IPA: ɛ/e"
...
	kIPA  = "IPA: k"
...
	dʒIPA = "IPA: d͡ʒ"
	tʃIPA = "IPA: t͡ʃ"

(Most of the phonemes in the above code were omitted here to conserve space.)

I am told, 'don't use leading k in Go names; var kIPA should be iPA'.

If you look at the pattern here, I am clearly not using Hungarian notation.

I think this shouldn't be marked as a problem.

@vikyd
Copy link

vikyd commented Oct 31, 2018

How about using k short for key? Such as looping a map:

for k, v := range params {
	kTrim := strings.TrimSpace(k)
	vTrim := strings.TrimSpace(v)
	delete(params, k)
	params[kTrim] = vTrim
}

@campoy
Copy link

campoy commented May 31, 2019

Just adding one more corner case in which this rule bit us, we wrote a function that implements a k shortest path routing algorithm and obviously we called it kShortestPath.

Unfortunately that's also mistakenly identified as Hungarian notation.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants