Skip to content

Instantly share code, notes, and snippets.

@isratmir
Created January 4, 2019 12:40
Show Gist options
  • Save isratmir/5c89a1524a7837aad91f34348f33230f to your computer and use it in GitHub Desktop.
Save isratmir/5c89a1524a7837aad91f34348f33230f to your computer and use it in GitHub Desktop.
//1.
//Every Go program is made up of packages.
//Programs start running in package main.
//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.
//2.
import "fmt"
import "math"
//parenthesized, "factored" import:
import (
"fmt"
"math"
)
//3.
//In Go, a name is exported if it begins with a capital letter.
//For example, Pizza is an exported name, as is Pi, which is exported from the math package.
//pizza and pi do not start with a capital letter, so they are not exported.
//When importing a package, you can refer only to its exported names.
//Any "unexported" names are not accessible from outside the package.
//4.
//A function can take zero or more arguments.
func add(x int, y int) int {
return x + y
}
//5.
//When two or more consecutive named function parameters share a type,
//you can omit the type from all but the last.
func add(x, y int) int {
return x + y
}
//6.
//A function can return any number of results.
func swap(x, y string) (string, string) {
return y, x
}
func main() {
a, b := swap("hello", "world")
fmt.Println(a, b)
}
//7.
//Named return values.
//A return statement without arguments returns the named return values. This is known as a "naked" return.
//Naked return statements should be used only in short functions, as with the example shown here.
//They can harm readability in longer functions.
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
//8.
//Variables
//The var statement declares a list of variables; as in function argument lists, the type is last.
//A var statement can be at package or function level. We see both in this example.
var c, python, java bool
func main() {
var i int
fmt.Println(i, c, python, java)
}
//9.
//Variables with initializers
//A var declaration can include initializers, one per variable.
//If an initializer is present, the type can be omitted; the variable will take the type of the initializer.
var i, j int = 1, 2
func main() {
var c, python, java = true, false, "no!"
fmt.Println(i, j, c, python, java)
}
//9.
//Short variable declarations
//Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.
//
//Outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available.
func main() {
var i, j int = 1, 2
k := 3
c, python, java := true, false, "no!"
//10.
//Basic types
//The example shows variables of several types, and also that variable declarations may
//be "factored" into blocks, as with import statements.
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8
rune // alias for int32
// represents a Unicode code point
float32 float64
complex64 complex128
//The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems
//and 64 bits wide on 64-bit systems. When you need an integer value you should
//use int unless you have a specific reason to use a sized or unsigned integer type.
//12.
//Zero values
//Variables declared without an explicit initial value are given their zero value.
func main() {
var i int
var f float64
var b bool
var s string
fmt.Printf("%v %v %v %q\n", i, f, b, s)
}
//13.
//Type conversions
//The expression T(v) converts the value v to the type T.
//Some numeric conversions:
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)
//Or, put more simply:
i := 42
f := float64(i)
u := uint(f)
//14.
//Type inference
//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.
//15.
//Constants
//Constants are declared like variables, but with the const keyword.
//
//Constants can be character, string, boolean, or numeric values.
//
//Constants cannot be declared using the := syntax.
const Pi = 3.14
func main() {
const World = "世界"
fmt.Println("Hello", World)
fmt.Println("Happy", Pi, "Day")
const Truth = true
fmt.Println("Go rules?", Truth)
}
//16.
//Numeric Constants
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment