Go coding guidelines

Overview

Go is an open source, procedural programming language. Programs are assembled by using packages, for efficient management of dependencies. This page contains the information and instructions on how to use Helper packages for Zabbix agent 2 parameter configuration for reusability of errors and connections, for handling error formatting, and also for configuration of the location of third-party libraries. See the list of Zabbix agent 2 Plugin Support packages. Section Plugin packages provides information on the most frequently used packages and is particularly explicit on zbxerr and third-party libraries.

See the full list of Zabbix source Go packages supported by current Go versions.

For more information on plugin installation, see Zabbix plugin development guidelines.

Plugin packages

Helper

Helper packages help in working of and keeping Zabbix agent 2 Go functionality consistent. The most commonly used packages are zbxerr, log and uri. The full list of packages utilized by Zabbix is given in the Overview section.

Zbxerr

Zbxerr package handles error formatting. The instructions on how to create a new error message are given below:

  1. To create a new error message:
zbxerr.New(err.Error())
  1. To create a custom error message:
zbxerr.New("custom error").Wrap(err)
  1. To minimize error text inconsistencies, it is recommended to use predefined error messages, already available by default in zbxerr package.
ErrorTooManyParameters
       ErrorInvalidParams
       ErrorTooFewParameters
       ErrorTooManyParameters 
  1. To create a more detailed error message, the cause of the error can be added at the end of the predefined error message:
zbxerr.ErrorCannotMarshalJSON.Wrap(err)

See the samples for Error messages and Zabbix naming guidelines

Third-party libraries

When it is necessary to add third-party libraries, it is done in a following way:

  1. Use the go get command from ~/zabbix/src/go directory:
go get example.com/pkg
  1. If a specific version or tag is required, use this one:
go get example.com/pkg@v1.2.3

Any additional plugin parameter configuration is also done in plugins. See the instructions below.

  1. This command will add the requested package to go.mod file:
go get
  1. To clean up and update go.sum, it might be necessary to run this one:
go mod tidy

Parameters

Parameters for Zabbix agent 2 are configured utilizing the native Go flag package. See the instructions for configuration below.

  1. At first, all the global parameters are defined:
    var testFlag string
           const (
               testDefault     = ""
               testDescription = "description"
           )
           flag.StringVar(&testFlag, "test", testDefault, testDescription)
           flag.StringVar(&testFlag, "t", testDefault, testDescription+" (shorthand)")
  1. Afterwards, the specific parameters of an operating system are added:
func loadOSDependentFlags() {}

Parameters for Zabbix agent 2 plugins are defined and handled separately for each plugin. Typically, they are checked for the allowed number of parameters and values.

Formatting

Follow the instructions given below.

  1. Go code must conform to the rules specified in a configuration file:
src/go/.golangci.yml
  1. Thus, the following line:
 make style NEW_FROM_REV=HEAD~X
  1. Must not produce warnings, for example, this one:
make style NEW_FROM_REV=HEAD~2

In this syntax, X denotes the oldest commit number from where the changes began.

In order to run the checks, it is required to install golangci-lint:.