Can someone give me an example of a well written simple Golang code?

Hi, can someone give me a link or even copy paste a very well-written SIMPLE Golang code that follows the formatting rules of the Go language? I don’t want something complicated but at the same time covers every or most of the formatting rules

Such as:
do variables use camel case as js or underscores? what about function names?
How about struct and interfaces? Does a method use self as python or the lower case of its struct or just the first letter? How many spaces are there between a function and other things? are they two as in python or what? and many more things like those

I know these things don’t really matter but I would like to be a professional with Go. Using underscores in JavaScript will be weird and make it harder to work with a team when everyone is going into his own of what he thinks is right

Thanks :slight_smile:

Idiomatic identifier names are camelcase.

The receiver of methods usually is the first or perhaps first two letters or some kind of 2, max 3 letter acronym of the receivers type name. Just long enough to be recognizable in the context.

Most other things you have asked are dealt by go fmt anyway, and you really should not not use it!

2 Likes

https://tour.golang.org

tool golint check your codes.

1 Like

How should the tool know if you want to export the field or not? Would you expect a Java formatter to add public and private qualifiers to your code?

1 Like

Have you gone through https://golang.org/doc/effective_go.html? It specifies all the do and don’ts in Go. It also has simple examples in source code format.

Unlike Javascript, Go is strict against some of your queries. Some are considered rules instead or norms. E.g: the first letter denotes the export/unexport attributes of an element (var, func, constant, etc).

Generally speaking, gophers adhere to gofmt and Effective Go for code coherences and effective communications (read codes) globally. Example: you can use a snake case convention despite not to (see: https://play.golang.org/p/sLKepHhyxnA) but in exchange, you broke the effective communications and other gophers might not be able to help you effectively as you grow your package.

Avoid transferring syntax formatting rules and conventions when you use multiple programming languages. Otherwise, you won’t be able to effectively use it to its highest potentials. Cheers!

2 Likes