`
sillycat
  • 浏览: 2539825 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Golang(2)Language Basic

 
阅读更多

Golang(2)Language Basic

>go install com/sillycat/easygoapp/math
>go install com/sillycat/easygoapp

The first command will generate the pkg directory, the second will generate the bin directory.

>go run src/com/sillycat/easygoapp/main.go

This command will directly run the go codes.

My first hello world Exmaple, main.go is as follow:
package main

import (
     "fmt"
     "math"
)

func main() {
     fmt.Printf("Hello, sillycat. \n")
     fmt.Printf("Math result = %v\n", math.Sqrt(10))
}

package main will generate a bin file. Other package will only generate *.a files. Every app should contains one package main and a function main without parameters and return values.

fmt.Printf   package_Name.function_Name

Define Variables
var variableName type
var variableName type = value
var vname1, vname2, vname3 (type)= v1, v2, v3

Or 

vname1, vname2, vname3 := v1, v2, v3

:= can be equals to var and type, but this can only be used in side the functions, the global variable should still use var.

Go will complain the Error if you define a variable but not using it.

Const

const constantName = value
const Pi float32 = 3.1415926

Private Type
var isActive bool //global variable
var enabled, disabled = true, false //type omitted
func test(){
     var available bool //normal definition
     valid := false         //short definition
     available = true    
}

Number 
rune, int8, int16, int32, int64, byte, uint8, uint16, uint32, uint64
rune = int32, byte = uint8

float32, float64, default is float64.

complex128, complex64.

String
no, yes, maybe := “no”, “yes”, “maybe”
string can not be changed, but alternatively, we can do something as follow>
s := “hello”
c := []byte(s)
c[0]=‘c’
s2 := string(c)

s: = “hello”
s = “c” + s[1:]

multiple lines strings
m : = ‘hello
              sillycat’

Error
err := errors.New(“error message”)
if err != nil {
     fmt.Print(err)
}

Group Import/Define
import(
     “fmt”
     “os"
)

const(
     i = 100
     pi = 3.14
)

var(
     i int
     pi float32
)

iota Enum
const(
     x = iota //x ==0
     y = iota //y == 1
     z = iota //z == 2
     w //means w = iota
)
const v = iota //every const will reset the iota, v == 0
const{
     e, f, g = iota, iota, iota // e= 0, f = 0, g = 0, iota will be the same value if the iota in same line
}

public property, public Function,  Capital first character
private property,private Function,  Lower case first character

array, slice, map
array
var arr [n]type     n for the length of the array, type for the type of the elements

For example:
var arr [10]int   //define an array with 10 int 
arr[0] = 42
arr[1] = 13

Since length is part of the array definition, [3]int and [4]int are different.

a := [3]int{1,2,3}
b :=[10]int{1,2,3} //first 3 elements are 1,2,3
c :=[…]int{4, 5, 6} //we omitted the length, go will count the elements.

Or array in array

doubleArray := [2][4]int{[4]int{1,2,3,4}, [4]int{5,6,7,8}}
easyArray := [2][4]int{{1,2,3,5},{5,6,7,8}}

slice
We do not need to put length there.
var fslice []int

slice := []byte {‘a’, ‘b’, ‘c’, ‘d’}

slice can be defined from array or slice, For example
var ar = [10]byte {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’ }

var a, b []byte // these are 2 slice variables

a = ar[2:5] //slice is from 2 to 5 from array, the index of the array should from 2, 3, 4(This is coming from j-1 )

ar[:n] equals to ar[0:n], ar[n:] equals to ar[n:len(ar)], ar[:] equals to ar[0:len(ar)]

slice is reference, so once the value changes, all the references refer to this value will change.

len will get the length of the slice
cap max size of the slice
append  append one element
copy   copy slice from src to diet

map
map[keyType]valueType

var numbers map[string] int
numbers := make(map[string]int)

numbers[“one”] = 1

there is no order in map.

rating := map[string]float32 {“c”:5, “go”:4.5}

//There are 2 return value from map
csharpRating, ok = rating[“c”]
if ok {
     fmt.Println(“I will get the value is “, cscharpRating)
} else {
     fmt.Println(“Nothing here")
}
delete(rating, “c")

I rewrite the Example as follow:
     rating := map[string]float32{"c": 5, "go": 4.5}
     csharpRating1, ok1 := rating["c"]
     if ok1 {
          fmt.Println("I will get the value is ", csharpRating1)
     } else {
          fmt.Println("Nothing here")
     }
     delete(rating, "c")
     csharpRating2, ok2 := rating["c"]
     if ok2 {
          fmt.Println("I will get the value is ", csharpRating2)
     } else {
          fmt.Println("Nothing here")
     }

The console output is as follow:
Hello, sillycat. Math result = 3.1622776601683795 I will get the value is  5 Nothing here

make and new operation
make(T, args) can only create slice, map and channel and return T, not *T
new(T)



Install Supervisor
>sudo easy_install supervisor


References:
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/02.0.md

http://supervisord.org/
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/12.3.md



分享到:
评论

相关推荐

    go_recipes.pdf

    Go, also commonly referred to as Golang, is a general-purpose programming language conceived at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. The language first appeared in November ...

    hprose-golang:Hprose是一种跨语言的RPC。 该项目是Gopro的Hprose

    Hprose支持多种编程语言,例如: 自动更快动作脚本ASPC ++ 镖德尔福/帕斯卡dotNET(C#,Visual Basic ...) 高朗JavaJavaScript Node.js 目标C PerlPHP PythonRuby... 通过Hprose,您可以方便有效地在这些编程语言...

    Go Cookbook 含代码

    Golang) is a statically-typed programming language first developed at Google. It is derived from C with additional features such as garbage collection, type safety, dynamic-typing capabilities, ...

    java简易版开心农场源码-back-end:后端技术栈

    Golang Perl Visual Basic Object Pascal TIOBE 8月编程语言排行1-20 PHP概念 PHP原始为Personal Home Page的缩写,已经正式更名为 "PHP: Hypertext Preprocessor"。 PHP官方解释 PHP is a popular general-purpose ...

    rmbasicx64:Windows和Linux的向后兼容RM Basic解释器

    RM BASICx64是RM Basic的重新实现,RM Basic是Research Machines于1985年为开发的BASIC方言,目标是64位Windows和Linux操作系统。 它是用Go语言编写的,并使用扩展程序来来模拟RM Nimbus的输入和输出。 高层目标: ...

    Head First Go 1st Edition

    This book will not only teach developers basic language features, it will get them comfortable consulting error output, documentation, and search engines to find solutions to problems. It will teach ...

    The way to go

    PART 2—CORE CONSTRUCTS AND TECHNIQUES OF THE LANGUAGE Chapter 4—Basic constructs and elementary data types.......................................................49 4.1. Filenames—Keywords—...

    [Go语言入门(含源码)] The Way to Go (with source code)

    PART 2—CORE CONSTRUCTS AND TECHNIQUES OF THE LANGUAGE Chapter 4—Basic constructs and elementary data types.......................................................49 4.1. Filenames—Keywords—...

Global site tag (gtag.js) - Google Analytics