go未入门学习记录
开发环境配置go1.12.17+goland
参考链接macos配置go语言以及goland开发环境
语法学习记录
package main
import (
"bufio"
"fmt"
"math"
"os"
"strings"
)
func foo()(string,int){
return "1234",900
}
func slice_sum(arr []int) (sum int) {
sum = 0
for _, elem := range arr {
sum += elem
}
return
}
func sum(base int, arr ...int) int {
sum := base
for _, val := range arr {
sum += val
}
return sum
}
func example() int{
var arr1 = []int{1, 2, 3, 4, 5}
var sum = func(arr ...int) int {
total_sum := 0
for _, val := range arr {
total_sum += val
}
return total_sum
}
fmt.Println(sum(arr1...))
return 0
}
func testDefer() {
fname := "D:\\Temp\\test.txt"
f, err := os.Open(fname)
defer f.Close()
if err != nil {
os.Exit(1)
}
bReader := bufio.NewReader(f)
for {
line, ok := bReader.ReadString('\n')
if ok != nil {
break
}
fmt.Println(strings.Trim(line, "\r\n"))
}
}
func test_panic() {
defer func() {
msg := recover()
fmt.Println(msg)
}()
fmt.Println("I am walking and singing...")
panic("It starts to rain cats and dogs")
}
func main(){
fmt.Println("Hello World!")
var name string
var(
a string = "111"
b int = 111
c bool = false
)
fmt.Println(a,b,c,name)
fmt.Printf("%s1234%d\n",a,b)
var y = 200
fmt.Println(y)
aaa:=123
fmt.Println(aaa)
aaaa,_:=foo()
fmt.Println(aaaa)
const pi float32 = 3.1415
const(
aaaaaa = 1000
bbbbb
ccccc
)
const(
a_ = iota
_ = iota
c_ = 1000
d_ = iota
)
const(
_ = iota
kb = 1<<(10*iota)
mb = 1<<(10*iota)
)
const(
a__,b__ = iota+1,iota+2
c__,d__ = iota+1,iota+2
)
var a1 int = 10
fmt.Printf("%d \n",a1)
var a2 int = 077
fmt.Printf("%o \n",a2)
var a3 int = 0xff
fmt.Printf("%x \n",a3)
fmt.Printf("%p \n",&a1)
fmt.Printf("%f \n",math.MaxFloat32)
fmt.Println("\"c:\\go\"")
var s = `多行
文本
不用转义
`
fmt.Println(s)
ss := fmt.Sprintf("%s%s",s,s)
fmt.Println(ss)
ret := strings.Split(ss,"行")
fmt.Println(ret)
retBool := strings.Contains(ss,"行")
fmt.Println(retBool)
ret3 := strings.HasPrefix(ss,"多")
ret4 := strings.HasSuffix(ss,"义")
fmt.Println(ret3,ret4)
fmt.Println(strings.Index(ss,"行"))
fmt.Println(strings.LastIndex(ss,"行"))
sss := []string{"11","22","33"}
fmt.Println(strings.Join(sss,"-"))
a_rune := '中'
fmt.Println(a_rune)
b_rune := "as中"
fmt.Println(len(b_rune))
for i:=0;i<len(b_rune);i++{
fmt.Printf("%c\n",b_rune[i])
}
for _,v:=range b_rune{
fmt.Sprintf("%c\n",v)
}
s1 := "big"
s2 := []byte(s1)
s2[0] = 'p'
fmt.Println(string(s2))
if 3>2{
fmt.Println("123")
}else {
fmt.Println("sas")
}
if age2:=100;age2>90 {
fmt.Println("21")
}
age := 19
for age>10{
age--
}
for{
if age>0 {
break
}
}
switch age{
case 1 :
fmt.Println("11")
case 2 :
fmt.Println("123")
default:
fmt.Println("asdas")
}
switch n:=7; n {
case 1,3,5,7,9:
fmt.Println("jishu")
fallthrough
case 2,4,6,8:
fmt.Println("oushu")
}
for i:=1;i<3;i++{
for j:=1;j<3;j++ {
if (i == 2 && j == 2) {
goto label
}
}
}
label:
var xarray = [5]int{}
var xarray1 = [5]int{1,2,3,4,5}
var xarray2 = [5]int{}
var xarray3 = [...]string{
"11","22","33",
}
var xyarray = [3][2]int{
[2]int{1,2},
[2]int{3,4},
[2]int{5,6},
}
fmt.Println(xarray,xarray1,xarray2,xarray3,xyarray)
var xsharp = make([]int,5)
var xsharp1 = make([]int,5,10)
fmt.Println(cap(xsharp),cap(xsharp1))
var s_1 = xarray1[:3]
fmt.Println(s_1)
slice1 := []int{1,2,3,4,5,6}
slice2 := make([]int,5,10)
copy(slice2,slice1)
var x_map = map[string]string{
"a":"apple",
"b":"banana",
"o":"orange",
}
var x_map2 map[string]string
x_map2 = make(map[string]string)
x_map2["a"] = "apple"
x_map2["b"] = "banana"
for key,val := range x_map{
fmt.Println(key,val)
}
fmt.Println(x_map["p"])
if val,ok := x_map["a"];ok{
fmt.Println(val)
}
delete(x_map,"a")
var x_ptr *int
x_val := 10
x_ptr = &x_val
fmt.Println(x_val)
fmt.Println(x_ptr)
fmt.Println(*x_ptr)
x_ptr_new := new(int)
*x_ptr_new = 10
}
type Rect struct {
width float64
length float64
}
func (rect Rect) area() float64 {
return rect.width * rect.length
}
func structptr() {
var rect = new(Rect)
rect.width = 100
rect.length = 200
fmt.Println("Width:", rect.width, "Length:", rect.length,
"Area:", rect.area())
}
func (rect *Rect) double_area() float64 {
rect.width *= 2
rect.length *= 2
return rect.width * rect.length
}
func ptrana() {
var rect = new(Rect)
rect.width = 100
rect.length = 200
fmt.Println(*rect)
fmt.Println("Double Width:", rect.width, "Double Length:", rect.length,
"Double Area:", rect.double_area())
fmt.Println(*rect)
}
type Phone struct {
price int
color string
}
func (phone Phone) ringing() {
fmt.Println("ringing")
}
type IPhone struct {
Phone
model string
}
func inner() {
var p IPhone
p.price = 5000
p.color = "Black"
p.model = "iPhone 5"
p.ringing()
fmt.Println("I have a iPhone:")
fmt.Println("Price:", p.price)
fmt.Println("Color:", p.color)
fmt.Println("Model:", p.model)
}
type Phonep struct{
price int
color string
}
type Myphone struct{
phone Phonep
model string
}
func inner2() {
var p Myphone
p.phone.price = 5000
p.phone.color = "Black"
p.model = "iPhone 5"
fmt.Println("I have a iPhone:")
fmt.Println("Price:", p.phone.price)
fmt.Println("Color:", p.phone.color)
fmt.Println("Model:", p.model)
}
type ppPhone interface {
call()
}
type NokiaPhone struct {
}
func (nokiaPhone NokiaPhone) call() {
fmt.Println("I am Nokia, I can call you!")
}
type iiIPhone struct {
}
func (iPhone iiIPhone) call() {
fmt.Println("I am iPhone, I can call you!")
}
func testInterface() {
var phone ppPhone
phone = new(NokiaPhone)
phone.call()
phone = new(iiIPhone)
phone.call()
}