Text file tour/basics.article

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

View as plain text