Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Style Guide #100

Closed
bufdev opened this issue Dec 16, 2015 · 9 comments
Closed

Proposal: Style Guide #100

bufdev opened this issue Dec 16, 2015 · 9 comments

Comments

@bufdev
Copy link
Contributor

bufdev commented Dec 16, 2015

This is going to be a running list over the next week or so, but to kick it off, here's some proposals:

  • All packages have a file named name_of_package.go, ie in api/server, we have server.go
  • Packages are named after their directory, ie api/ is package api
  • I'm 50/50 on this: sub-packages (sub-directories) concatenate their parent package (parent directory) name with their name to make their package name, ie api/server is package apiserver, and then we will have apiserver.go.
  • ALL PUBLIC TYPES GO IN name_of_package.go. Every other file is just a helper file that implements the types.
  • Structs without a corresponding interface are data holders. Structs with functions attached have a public interface wrapper, and then the struct becomes private. Example:
// foo.go
package foo

type Runner interace {
  Run(one string, i int) error
}

func NewRunner(something bar.Something) Runner {
  return newRunner(something)
}

// runner.go
package foo

type runner struct{
  something bar.Something
}

func newRunner(something bar.Something) *runner {
  return &runner{something}
}

func (r *runner) Run(one string, i int) error {
  r.hello(i)
  return r.something.Bar(one, i+1)
}

func (r *runner) hello(i int) {
  return r.something.Hello(i)
}
// no
err := foo()
if err != nil {
  return nil, err
}
// yes
if err := foo(); err != nil {
  return nil, err
}

Other things:

  • Code checked in that has warnings https://golang.org/cmd/cgo/ is disallowed
  • Code checked in that does not pass make build is disallowed
  • Code checked in that does not pass make docker-test is disallowed
  • Code always includes relevant vendoring (make docker-test takes care of this basically)
  • Code that breaks the build is immediately fixed
  • Once completed, go vet, errcheck, and golint fails are considered broken code
  • Disallowed = big time scolding and/or rollback

Thoughts?

@bufdev
Copy link
Contributor Author

bufdev commented Dec 16, 2015

@jvinod @gourao

@bufdev
Copy link
Contributor Author

bufdev commented Dec 16, 2015

An example of most of the things here: https://github.com/peter-edge/go-protolog

@gourao
Copy link
Contributor

gourao commented Dec 17, 2015

+1. Also congrats on PR100!

@jvinod
Copy link
Contributor

jvinod commented Dec 17, 2015

all proposals LGTM ..will comment on individual requests separately. w.r.t sub directories concatenating parent dir, I'd say no. Each dir should behave like an orphan

@bufdev
Copy link
Contributor Author

bufdev commented Dec 21, 2015

Other things, more for myself to note:

  • Actual names for variables, api.Volume is not v, vol, whatever, its volume, a request is not req, createReq, r, its request
  • Pointers instead of structs
  • function parameters/struct initialization/function calls etc are either on one line or each parameter/field has a new line for it

@bufdev
Copy link
Contributor Author

bufdev commented Dec 21, 2015

File order:

package pkg

const (
  ...
)

var (
 ...
)

// public struct

// public struct functions

// private struct functions

// private functions but only if they just apply to this struct, otherwise in a common file

@bufdev
Copy link
Contributor Author

bufdev commented Dec 21, 2015

Empty structs:

// no
type EmptyStruct {
}
// yes
type EmptyStruct {}

@bufdev
Copy link
Contributor Author

bufdev commented Jan 22, 2016

fa57fcf

@jvinod
Copy link
Contributor

jvinod commented Feb 3, 2016

merged

@jvinod jvinod closed this as completed Feb 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants