Getting started
Basic types
Variables
Constants
Strings
Pointers
Arrays
Slices
Maps
Structs
Interfaces
Empty interface
if, switch, goto
for, while loops
range statement
Functions
Methods
Error handling
Defer
Panic and recover
Concurrency
Channels and select
Mutex
Packages
Files and I/O
Time and date
Command line arguments
Logging
Executing commands
Hex, base64 encoding
JSON
XML
CSV
YAML
SQL
HTTP Client
HTTP Server
Text and HTML templates
Reflection
Context
Package fmt
OS Signals
Testing
Calling C from GO with cgo
Profiling using go tool pprof
Cross compilation
Conditional compilation with build tags
Inlining functions
sync.Pool for better performance
gob
Plugin
HTTP server middleware
Protobuf in Go
Console I/O
Cryptography
Images (PNG, JPEG, BMP, TIFF, WEBP, VP8, GIF)
The Go Command
Testing code with CI services
Windows GUI programming
Contributors

Constants

suggest change

Go supports constants of character, string, boolean, and numeric values.

Constant basics:

// Greeting is an exported (public) string constant
const Greeting string = "Hello World"

// we can group const declarations
const (
	// years is an unexported (package private) int constant
	years int  = 10
	truth bool = true
)

Constants can be used like variables, except that their value can't change.

An example:

// collection: Essential Go
package main

import (
	"fmt"
	"math"
)

const s string = "constant"

func main() {
	fmt.Println(s) // constant

	// A `const` statement can appear anywhere a `var` statement can.
	const n = 10
	fmt.Println(n)                           // 10
	fmt.Printf("n=%d is of type %T\n", n, n) // n=10 is of type int

	const m float64 = 4.3
	fmt.Println(m) // 4.3

	// An untyped constant takes the type needed by its context.
	// For example, here `math.Sin` expects a `float64`.
	const x = 10
	fmt.Println(math.Sin(x)) // -0.5440211108893699
}
constant
10
n=10 is of type int
4.3
-0.5440211108893699

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents