Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go Naming convention for function

In documentation, they use camel case and pascal case both. Is this convention different for functions and methods? Does it decide scope of a function ?

like image 295
surbhi Avatar asked Sep 11 '26 17:09

surbhi


2 Answers

Yes. In Go a field/method is exported if it begins with an upper cased letter while it is unexported if the first letter of the identifier is lower cased. This is similar to the public/private features in most OO languages. Here's an example or two;

package "a"

func ThisFunctionIsExported() {}

func thisOneIsNot() {}


...

package "b"

import "a"

a.ThisFunctionIsExported() // works

a.thisOneIsNot() // compiler error

So, yes the developer is consciously deciding what the scope of those methods are with their casing choice. Lower cased functions are always helper methods inside the package scope, they aren't exposed in importing scopes.

like image 73
evanmcdonnal Avatar answered Sep 14 '26 02:09

evanmcdonnal


I'm not sure an documentation on Go ever mentions "Pascal case". Supposedly you're using this term to denote camel cased name which begins with either a capital or small letter, right?

Okay, Go uses the term "camel case" for both variations. The distinction between the first letter of an identifier being capital or not does indeed govern the visibility of that symbol. Please read any entry-level guide on Go for more info.

like image 32
kostix Avatar answered Sep 14 '26 01:09

kostix