Exemplo n.º 1
0
// Last in a file should be a main func. This func should only be present in a
// package named main to generate a binary file.
func main() {
	// Break long function calls into its multi-line form. Functions with lots of
	// parameters are often a code smell.
	example := conventions.NewExample(
		1,
		"conventions",
		"this is getting quite meta",
	)

	// If there is no desire to expose any variables outside of the scope of an
	// if-statement, this two-clause form is preferable.
	if _, err := example.Method(); err != nil {
		// Panics are used only in situations that inform the programmer that they
		// are doing something wrong. They should not be used like exceptions --
		// error is a builtin type for a reason. To make life easier, panic strings
		// should be prefixed with "pkgname: ".
		panic("conventions: example method should always return a nil error.")
	}

	// When a zero-value is desired, use the var keyword. Avoid using := inside
	// if statements as variables can accidentally become shadowed.
	var multiplier int
	if example.ID > 10 {
		multiplier = 10
	} else {
		multiplier = example.ID
	}
	fmt.Printf("Multiplier: %v\n", multiplier)
}
Exemplo n.º 2
0
func ExampleNew() {
	example := conventions.NewExample(
		1,
		"conventions",
		"this is getting quite meta",
	)
	fmt.Println(example.ID)
	// Output:
	// 1
}