My journey to learn Go:Day1

228 阅读2分钟

#Hello,Golang!

Let's Go

Links:

  1. Tour of Go

Packages

  • Go program is made of packages.
  • Program start running in packages main.
  • The package name is the same as the last element of the import path.

Import

  • It's better to use the factored import statement than the multiple statement.
import (
	"fmt"
	"math"
)
import "fmt"
import "math"

Expored names

  • When importing a package ,you can refer only to exported names.Any "unexported" names are not accessible from outside the package.

Functions

  • A function can take zero or more arguments.
  • The type comes after the avalible name.(Please pay attention!)
func add(x int,y int, z int) int{
	return x+y+z
} 
  • When two or more consecutive named function parameters share a type,you can omit the type all but the last.
func add(x, y int, z bool) int {
return x+y
}
  • A function can return any number of results.
func swap(x int, y string) (string, int) {
	return y, x
}

Named return values

  • The return name can be named.If so,they are treated as variables definded at the top of the function.
  • The return statement without arguments("naked" return,which will harm readablity in longer functions) returns the named return values.
func Subtract(x,y int)(z int){
	z = x -y
    return
}

Variables and Initializers

  • The var statement daclares a list of variables.
  • Inr function argument lists,the type is last.
  • A var statement can be at function or package level.
  • 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.
import 	"fmt"
var i, j int = 1, 2
func main() {
	var c, python, java = true, false, "no!"
	fmt.Println(i, j, c, python, java)
}

Short variable declarations

  • Inside the function,the := short assignment statement can be used in place of a var declartion with implicit type.
  • Outside a function,every statement begins with keys(var,func and so on) and so the := construct is not available
import "fmt"
// i := 1  This is not avalible!
func main() {
	var i, j int = 1, 2
	k := 3
	e,t := "OJ","Bk" 
	c, python, java := true, false, "no!"
	fmt.Println(e,t)
	fmt.Println(i, j, k, c, python, java)
}

Basic types

  • Variable declarations may be "factored" into block,as with the import statements.

bool

string

int int8 int16 int32 int64

byte//alias for uint8

rune//alias for int32;represents a Unicode code point

float32 float64

complex64 complex128

import (
   "fmt"
   "math/cmplx"
)
var (
   a bool = false
   b string ="Hi,Go!"
   c int = -12345
   d uint64 = 1<<64 - 1
   e byte = 123
   f rune = 2
   g float32 = 1.5
   h complex128 = cmplx.Sqrt(-5 + 12i)
)
func main() {
   fmt.Printf("Type: %T Value: %v\n", a, a)
   fmt.Printf("Type: %T Value: %v\n", b, b)
   fmt.Printf("Type: %T Value: %v\n", c, c)
   fmt.Printf("Type: %T Value: %v\n", d, d)
   fmt.Printf("Type: %T Value: %v\n", e, e)
   fmt.Printf("Type: %T Value: %v\n", g, g)
   fmt.Printf("Type: %T Value: %v\n", h, h)
}

Zero values

Variables declared without an explicit intital value are given their zero value.


func main() {
	var i int // 0
	var f float64 // 0
	var b bool // false 
	var s string // ""
	fmt.Printf("%v %v %v %q\n", i, f, b, s)
}

Type conversions

The expresssion convert T(v) the value to type T.

import (
	"fmt"
	"math"
)
func main() {
	var x, y int = 3, 4
	var f float64 = math.Sqrt(float64(x*x + y*y))
	var z uint = uint(f)
	fmt.Println(x, y, z)
}

Constants

  • Constants are declared liked variables,but with the const keyword.
const pi = 3.14
func main(){
const d = 1
}