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
- 浏览: 2539825 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 467NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 328Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 376Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 464NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 413Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 330Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 242GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 443GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 320GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 285Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 302Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 276NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 254Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 564NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 255Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 356Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 363Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
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支持多种编程语言,例如: 自动更快动作脚本ASPC ++ 镖德尔福/帕斯卡dotNET(C#,Visual Basic ...) 高朗JavaJavaScript Node.js 目标C PerlPHP PythonRuby... 通过Hprose,您可以方便有效地在这些编程语言...
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, ...
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 ...
RM BASICx64是RM Basic的重新实现,RM Basic是Research Machines于1985年为开发的BASIC方言,目标是64位Windows和Linux操作系统。 它是用Go语言编写的,并使用扩展程序来来模拟RM Nimbus的输入和输出。 高层目标: ...
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 ...
PART 2—CORE CONSTRUCTS AND TECHNIQUES OF THE LANGUAGE Chapter 4—Basic constructs and elementary data types.......................................................49 4.1. Filenames—Keywords—...
PART 2—CORE CONSTRUCTS AND TECHNIQUES OF THE LANGUAGE Chapter 4—Basic constructs and elementary data types.......................................................49 4.1. Filenames—Keywords—...