1

【基础】Golang入门

 2 years ago
source link: https://wintrysec.github.io/2021/09/26/%E3%80%905%E3%80%91%E5%AE%89%E5%85%A8%E5%BC%80%E5%8F%91/Golang/%E3%80%90%E5%9F%BA%E7%A1%80%E3%80%91Golang%E5%85%A5%E9%97%A8/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

什么是Golang

Golang是编译型语言,被誉为21世纪的C语言。

Golang语法简洁且规范,可以快速的开发高性能的应用程序。

因为语法类似C语言,并且保留了指针便于内存操作,Go从1.5版本开始,默认采用多核执行,默认是你的CPU核心数。Go 语言是一门类型安全和内存安全的编程语言,虽然 Go 语言中仍有指针的存在,但并不允许进行指针运算。

Go 语言的另一个目标是对于网络通信、并发和并行编程的极佳支持,从而更好地利用大量的分布式和多核的计算机。

从 2010 年 5 月起,谷歌开始将 Go 语言投入到后端基础设施的实际开发中

Docker(容器技术)、Kubernetes(容器编排)、今日头条服务端等项目都使用Golang开发

简单而易用的爬虫框架 Colly

安装Golang环境

从这里https://go-zh.org/doc/install下载安装包,安装Golang环境

安装完成后用go version验证一下即可

编辑器推荐Goland

第一个Go程序

package main

import "fmt"

func main() {
var a string = "Hello, World"
fmt.Println(a)
}

一个Go程序由以下五部分组成

包声明
包引入
函数
变量
语句

运行Go程序

go run main.go

Windows下多平台交叉编译

#windows
go build main.go

#Linux
SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build main.go

#MAC
SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build main.go

在 Go 程序中,我们通常需要将数据由一种类型转换为另一种类型;

标准库 strconv 提供了字符串与基本数据类型之间的转换功能。

package main

import (
"fmt"
"strconv"
)


func main() {
var a int = 123
b := "xkx"
v := "10"

//整型数字转字符串,int -> str
rs := strconv.Itoa(a)+b
fmt.Println("int -> str:",rs)

//字符串转整型数字,str -> int
if s,err := strconv.Atoi(v);err == nil{
fmt.Println("str -> int",s)
fmt.Printf("转换后的类型是:%T",s)
}

}

cast是一个小巧、实用的类型转换库,用于将一个类型转为另一个类型。

$ go get github.com/spf13/cast
import (
"fmt"

"github.com/spf13/cast"
)

使用方法:

https://studygolang.com/articles/26433

Golang学习资源

1、Golang中文官网(官方文档)

1)Go编程语言规范
2)实效Go编程

2、Golang标准库(中文版)

3、Black-Hat-GO(中文在线阅读)

4、Golang安全资源合集


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK