Golang(2)Language Basic

本文介绍了Golang的基础知识,包括环境搭建、基本语法如变量定义、常量使用、数据类型介绍等,并通过一个简单的示例程序展示了如何运行Go代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值