github.com/brownsys/tracing-framework-go@v0.0.0-20161210174012-0542a62412fe/go/darwin_amd64/misc/tour/content/basics.article (about)

     1  Packages, variables, and functions.
     2  Learn the basic components of any Go program.
     3  
     4  The Go Authors
     5  https://golang.org
     6  
     7  * Packages
     8  
     9  Every Go program is made up of packages.
    10  
    11  Programs start running in package `main`.
    12  
    13  This program is using the packages with import paths `"fmt"` and `"math/rand"`.
    14  
    15  By convention, the package name is the same as the last element of the import path. For instance, the `"math/rand"` package comprises files that begin with the statement `package`rand`.
    16  
    17  #appengine: *Note:* the environment in which these programs are executed is
    18  #appengine: deterministic, so each time you run the example program
    19  #appengine: `rand.Intn` will return the same number.
    20  #appengine:
    21  #appengine: (To see a different number, seed the number generator; see [[https://golang.org/pkg/math/rand/#Seed][`rand.Seed`]].)
    22  
    23  .play basics/packages.go
    24  
    25  * Imports
    26  
    27  This code groups the imports into a parenthesized, "factored" import statement. 
    28  
    29  You can also write multiple import statements, like:
    30  
    31  	import "fmt"
    32  	import "math"
    33  
    34  But it is good style to use the factored import statement.
    35  
    36  .play basics/imports.go
    37  
    38  * Exported names
    39  
    40  In Go, a name is exported if it begins with a capital letter.
    41  For example, `Pizza` is an exported name, as is `Pi`, which is exported from
    42  the `math` package.
    43  
    44  `pizza` and `pi` do not start with a capital letter, so they are not exported.
    45  
    46  When importing a package, you can refer only to its exported names.
    47  Any "unexported" names are not accessible from outside the package.
    48  
    49  Run the code. Notice the error message.
    50  
    51  To fix the error, rename `math.pi` to `math.Pi` and try it again.
    52  
    53  .play basics/exported-names.go
    54  
    55  * Functions
    56  
    57  A function can take zero or more arguments.
    58  
    59  In this example, `add` takes two parameters of type `int`.
    60  
    61  Notice that the type comes _after_ the variable name.
    62  
    63  (For more about why types look the way they do, see the [[https://blog.golang.org/gos-declaration-syntax][article on Go's declaration syntax]].)
    64  
    65  .play basics/functions.go
    66  
    67  * Functions continued
    68  
    69  When two or more consecutive named function parameters share a type, you can omit the type from all but the last.
    70  
    71  In this example, we shortened
    72  
    73  	x int, y int
    74  
    75  to
    76  
    77  	x, y int
    78  
    79  .play basics/functions-continued.go
    80  
    81  * Multiple results
    82  
    83  A function can return any number of results.
    84  
    85  The `swap` function returns two strings.
    86  
    87  .play basics/multiple-results.go
    88  
    89  * Named return values
    90  
    91  Go's return values may be named. If so, they are treated as variables defined at the top of the function.
    92  
    93  These names should be used to document the meaning of the return values.
    94  
    95  A `return` statement without arguments returns the named return values. This is known as a "naked" return.
    96  
    97  Naked return statements should be used only in short functions, as with the example shown here. They can harm readability in longer functions.
    98  
    99  .play basics/named-results.go
   100  
   101  * Variables
   102  
   103  The `var` statement declares a list of variables; as in function argument lists, the type is last.
   104  
   105  A `var` statement can be at package or function level. We see both in this example.
   106  
   107  .play basics/variables.go
   108  
   109  * Variables with initializers
   110  
   111  A var declaration can include initializers, one per variable.
   112  
   113  If an initializer is present, the type can be omitted; the variable will take the type of the initializer.
   114  
   115  .play basics/variables-with-initializers.go
   116  
   117  * Short variable declarations
   118  
   119  Inside a function, the `:=` short assignment statement can be used in place of a `var` declaration with implicit type.
   120  
   121  Outside a function, every statement begins with a keyword (`var`, `func`, and so on) and so the `:=` construct is not available.
   122  
   123  .play basics/short-variable-declarations.go
   124  
   125  * Basic types
   126  
   127  Go's basic types are
   128  
   129  	bool
   130  
   131  	string
   132  
   133  	int  int8  int16  int32  int64
   134  	uint uint8 uint16 uint32 uint64 uintptr
   135  
   136  	byte // alias for uint8
   137  
   138  	rune // alias for int32
   139  	     // represents a Unicode code point
   140  
   141  	float32 float64
   142  
   143  	complex64 complex128
   144  
   145  The example shows variables of several types,
   146  and also that variable declarations may be "factored" into blocks,
   147  as with import statements.
   148  
   149  The `int`, `uint`, and `uintptr` types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems.
   150  When you need an integer value you should use `int` unless you have a specific reason to use a sized or unsigned integer type.
   151  
   152  .play basics/basic-types.go
   153  
   154  * Zero values
   155  
   156  Variables declared without an explicit initial value are given their
   157  _zero_value_.
   158  
   159  The zero value is:
   160  
   161  - `0` for numeric types,
   162  - `false` for the boolean type, and
   163  - `""` (the empty string) for strings.
   164  
   165  .play basics/zero.go
   166  
   167  * Type conversions
   168  
   169  The expression `T(v)` converts the value `v` to the type `T`.
   170  
   171  Some numeric conversions:
   172  
   173  	var i int = 42
   174  	var f float64 = float64(i)
   175  	var u uint = uint(f)
   176  
   177  Or, put more simply:
   178  
   179  	i := 42
   180  	f := float64(i)
   181  	u := uint(f)
   182  
   183  Unlike in C, in Go assignment between items of different type requires an
   184  explicit conversion.
   185  Try removing the `float64` or `uint` conversions in the example and see what happens.
   186  
   187  .play basics/type-conversions.go
   188  
   189  * Type inference
   190  
   191  When declaring a variable without specifying an explicit type (either by using the `:=` syntax or `var`=` expression syntax), the variable's type is inferred from the value on the right hand side.
   192  
   193  When the right hand side of the declaration is typed, the new variable is of that same type:
   194  
   195  	var i int
   196  	j := i // j is an int
   197  
   198  But when the right hand side contains an untyped numeric constant, the new variable may be an `int`, `float64`, or `complex128` depending on the precision of the constant:
   199  
   200  	i := 42           // int
   201  	f := 3.142        // float64
   202  	g := 0.867 + 0.5i // complex128
   203  
   204  Try changing the initial value of `v` in the example code and observe how its type is affected.
   205  
   206  .play basics/type-inference.go
   207  
   208  * Constants
   209  
   210  Constants are declared like variables, but with the `const` keyword.
   211  
   212  Constants can be character, string, boolean, or numeric values.
   213  
   214  Constants cannot be declared using the `:=` syntax.
   215  
   216  .play basics/constants.go
   217  
   218  * Numeric Constants
   219  
   220  Numeric constants are high-precision _values_.
   221  
   222  An untyped constant takes the type needed by its context.
   223  
   224  Try printing `needInt(Big)` too.
   225  
   226  (An `int` can store at maximum a 64-bit integer, and sometimes less.)
   227  
   228  .play basics/numeric-constants.go
   229  
   230  * Congratulations!
   231  
   232  You finished this lesson!
   233  
   234  You can go back to the list of [[/list][modules]] to find what to learn next, or continue with the [[javascript:click('.next-page')][next lesson]].