加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang(2)Language Basic

发布时间:2020-12-16 18:54:32 所属栏目:大数据 来源:网络整理
导读: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 c

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 = %vn",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,vname3 := v1,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,3} //first 3 elements are 1,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,3,4},[4]int{5,6,7,8}}
easyArray := [2][4]int{{1,5},{5,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’,‘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,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读