网站改版需要注意什么,seo短视频加密路线,网页设计与制作有什么感想,网站企业一、创建工程 说明#xff1a; #xff08;1#xff09;go.mod文件是go项目依赖管理文件#xff0c;相当于前端的package.json#xff0c;也就是Java项目中的Maven的pom.xml。
二、打印数据到控制台
#xff08;1#xff09;引入fmt #xff08;2#xff09;使用fmt…一、创建工程 说明 1go.mod文件是go项目依赖管理文件相当于前端的package.json也就是Java项目中的Maven的pom.xml。
二、打印数据到控制台
1引入fmt 2使用fmt.Sprintf参数为打印的变量非必须 3用Println()打印 例如
package mainimport(
fmt
)func main(){
//%d表示整型数字%s表示字符串
var stockcode123
var enddate2020-12-31
var urlCode%dendDate%s
var target_urlfmt.Sprintf(url,stockcode,enddate)
fmt.Println(target_url)//输出结果为Code123endDate2020-12-31
}Go字符串格式化符号 %v 按值的本来值输出 %v在%v的基础上对结构体字段名和值进行展开 %#v输出Go语言语法格式的值 %T输出Go语言语法格式的类型和值 %%输出%本体 %b类型以二进制方式显示 %o类型以八进制的方式显示 %d类型以十进制的方式显示 %x类型以十六进制的方式显示 %X整数以十六进制、字母大写方式显示 %UUnicode字符 %f浮点数 %p指针十六进制方式显示
三、Go语言的变量
3.1 变量类型
3.1.1 基本类型
整型int8、int16、int32、int64、uint8、uint16、uint32、uint64 浮点型float32、float64 复数型complex64、complex128 其他常用数字类型byte(uint8)、int[32|64]、 字节类型rune(int32)类似与其他语言里的char可以表示任何Unicode字符包括ASCII字符、中文字符、特殊符号等 字符串型string默认值为 布尔型bool值为true或false默认值为false
3.1.2 派生类型
指针
var a10;var b *int a;数组和切片 定义数组
var arr [2]int [2]int{2, 3, 4}定义切片var sli []int []int{}定义了一个变量sli类型为切片slice的整型切片 映射字典map 例如
var a map[String]int//声明了一个变量a类型为map键类型为字符串值类型为整型结构体
type User struct {Id intUsername stringAge byte}var user Useruser.Id 203.2 变量声明
3.2.1 完整声明
Go语言变量名由字母、数字、下划线组成其中首个字符不能为数字。 声明变量的一般形式是使用var关键字。 语法 var 变量名 类型; 可以一次声明多个变量 var 变量名1,变量名2 类型; 例如
var a string ABC
var b,c int 1, 2注意如果没有初始化则变量默认值为零。
3.2.2 根据值自动判定变量类型
var d true上述例子的var有些多余可以用赋值操作符来简写称之为“初始化声明”
b : true注意初始化声明只能被用在函数体内而不可以用于全局变量的声明和赋值。
3.3 Go语言常量
使用关键字const替代var常量的数据类型只能是基本类型可以省略数据类型。
const b stringabc
const b abc3.4 关键字go
go关键字用来启动一个新的goroutine。
package mainimport (fmttime
)func hello() {fmt.Println(Hello world goroutine)
}func main() {go hello() // 启动新的 Goroutinefmt.Println(Hello main goroutine)time.Sleep(1 * time.Second) // 等待新的 Goroutine 执行完成
}执行结果如下
3.5 Go的通道chan
在 Go 语言中chan 关键字用于创建通道channel通道是一种用于在 goroutine 之间进行通信的类型化管道。通过通道你可以在不同的 goroutine 之间安全地传递数据。 1使用make函数来创建通道 通道的类型由其传递的数据类型决定。 2发送和接受数据 发送数据通道 - 值 接收数据值 : -通道 例如
ch : make(chan int) // 在一个 goroutine 中发送数据
go func() { ch - 42
}() // 在主 goroutine 中接收数据
value : -ch
fmt.Println(value) // 输出: 423带缓冲的通道 默认情况下通道是无缓冲的这意味着发送操作会阻塞直到另一方准备好接收数据。你可以通过提供一个缓冲区大小来创建一个带缓冲的通道。 例如
ch : make(chan int, 2) // 创建一个带缓冲区的通道可以存储 2 个整数带缓冲的通道在缓冲区未满时不会阻塞发送操作在缓冲区为空时不会阻塞接收操作。 4关闭通道 你可以使用 close 函数来关闭一个通道。关闭通道后无法再向通道发送数据但可以继续从通道接收数据直到通道为空。 例如
ch : make(chan int) go func() { ch - 42 close(ch)
}() value, ok : -ch
if ok { fmt.Println(value) // 输出: 42
} else { fmt.Println(通道已关闭)
}3.6 range关键字
在 Go 语言中range 关键字具有多重用途主要用于遍历数组、切片slice、映射map、字符串以及通道channel。下面分别介绍这些用法
遍历数组和切片 对于数组和切片range 会返回两个值索引和对应位置的元素值。
numbers : []int{1, 2, 3, 4, 5}
for index, value : range numbers { fmt.Println(index, value)
}在上面的例子中index 是元素的索引而 value 是对应索引的元素值。
遍历映射 对于映射range 会返回两个值键和对应的值。
scores : map[string]int{Alice: 90, Bob: 85}
for key, value : range scores { fmt.Println(key, value)
}在这个例子中key 是映射的键而 value 是与键关联的值。
遍历字符串 对于字符串range 会返回两个值字符的索引字节位置和对应的 Unicode 码点rune。
str : Hello, 世界
for index, runeValue : range str { fmt.Printf(%#U starts at byte position %d\n, runeValue, index)
}注意对于非 ASCII 字符如中文一个字符可能会占用多个字节。因此这里的索引是指字节在字符串中的位置而不是字符在字符串中的位置对于多字节字符后者可能更有用但 Go 的 range 在字符串上不提供这种索引。
遍历通道 对于通道range 会持续地从通道中接收数据直到通道被关闭。
ch : make(chan int)
go func() { for i : 0; i 5; i { ch - i } close(ch)
}() for value : range ch { fmt.Println(value)
}在这个例子中range 会阻塞直到从通道 ch 中接收到数据。当通道被关闭且没有更多数据可读时range 循环会结束。
注意事项 当使用 range 遍历映射时遍历的顺序是随机的每次运行程序时可能会得到不同的顺序。 在遍历数组、切片或字符串时如果你只需要索引或值中的一个可以使用下划线 _ 来忽略另一个。 在遍历通道时确保通道最终会被关闭否则 range 循环将永远阻塞。
range 在内部使用了值拷贝因此遍历过程中修改元素的值对于数组和切片不会影响原始数组或切片。然而如果你传递的是一个指向元素的指针例如切片中的元素是指针类型则可以通过指针修改原始数据。
3.7 Go语言select语句
select 语句允许你在多个通道操作上进行等待。select 会阻塞直到其中一个 case 可以运行。如果有多个 case 都准备好了select 会随机选择一个执行。如果所有的通道都没有准备好就会执行default块中的代码。
ch1 : make(chan string)
ch2 : make(chan string) go func() { ch1 - 来自 ch1 的消息
}() go func() { ch2 - 来自 ch2 的消息
}() for i : 0; i 2; i { select { case msg1 : -ch1: fmt.Println(收到:, msg1) case msg2 : -ch2: fmt.Println(收到:, msg2) default://代码}
}执行结果;
四、Go语言的特点
1并发支持 内置轻量级的并发机制称为goroutine,可以通过goroutine和通道方便地编写并发程序。 2高性能 通过优化编译器和运行时环境以及并发机制地支持提供了出色地性能。 3内存安全 具有内置地垃圾回收机制避免了常见的内存错误。 4跨平台 编译器可以将Go代码编译为机器码支持多种操作系统和体系结构。 5丰富的标准库 涵盖了网络编程、文件操作、加密解密并发编程等各个方面。 文章转载自: http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.bpmfz.cn.gov.cn.bpmfz.cn http://www.morning.kltmt.cn.gov.cn.kltmt.cn http://www.morning.tgydf.cn.gov.cn.tgydf.cn http://www.morning.plqhb.cn.gov.cn.plqhb.cn http://www.morning.jtmrx.cn.gov.cn.jtmrx.cn http://www.morning.byrlg.cn.gov.cn.byrlg.cn http://www.morning.plqsc.cn.gov.cn.plqsc.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.gkmwk.cn.gov.cn.gkmwk.cn http://www.morning.fcwb.cn.gov.cn.fcwb.cn http://www.morning.kwblwbl.cn.gov.cn.kwblwbl.cn http://www.morning.tpmnq.cn.gov.cn.tpmnq.cn http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn http://www.morning.mzkn.cn.gov.cn.mzkn.cn http://www.morning.lptjt.cn.gov.cn.lptjt.cn http://www.morning.jbblf.cn.gov.cn.jbblf.cn http://www.morning.daxifa.com.gov.cn.daxifa.com http://www.morning.txhls.cn.gov.cn.txhls.cn http://www.morning.ctxt.cn.gov.cn.ctxt.cn http://www.morning.cnfjs.cn.gov.cn.cnfjs.cn http://www.morning.nfks.cn.gov.cn.nfks.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.xrct.cn.gov.cn.xrct.cn http://www.morning.lksgz.cn.gov.cn.lksgz.cn http://www.morning.rrgm.cn.gov.cn.rrgm.cn http://www.morning.fgxws.cn.gov.cn.fgxws.cn http://www.morning.flxqm.cn.gov.cn.flxqm.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.gcfrt.cn.gov.cn.gcfrt.cn http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn http://www.morning.frqtc.cn.gov.cn.frqtc.cn http://www.morning.mqfhy.cn.gov.cn.mqfhy.cn http://www.morning.nxkyr.cn.gov.cn.nxkyr.cn http://www.morning.pyncm.cn.gov.cn.pyncm.cn http://www.morning.hsjfs.cn.gov.cn.hsjfs.cn http://www.morning.kfcz.cn.gov.cn.kfcz.cn http://www.morning.ysjjr.cn.gov.cn.ysjjr.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.qxnns.cn.gov.cn.qxnns.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.kkwbw.cn.gov.cn.kkwbw.cn http://www.morning.ebpz.cn.gov.cn.ebpz.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.nzcys.cn.gov.cn.nzcys.cn http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.wsxxq.cn.gov.cn.wsxxq.cn http://www.morning.xptkl.cn.gov.cn.xptkl.cn http://www.morning.gtmdq.cn.gov.cn.gtmdq.cn http://www.morning.qgcfb.cn.gov.cn.qgcfb.cn http://www.morning.homayy.com.gov.cn.homayy.com http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.wgzgr.cn.gov.cn.wgzgr.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.djmdk.cn.gov.cn.djmdk.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.lrybz.cn.gov.cn.lrybz.cn http://www.morning.zlzpz.cn.gov.cn.zlzpz.cn http://www.morning.ffksr.cn.gov.cn.ffksr.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.fdmtr.cn.gov.cn.fdmtr.cn http://www.morning.rwjfs.cn.gov.cn.rwjfs.cn http://www.morning.mkyxp.cn.gov.cn.mkyxp.cn http://www.morning.tnktt.cn.gov.cn.tnktt.cn http://www.morning.dcdhj.cn.gov.cn.dcdhj.cn http://www.morning.mywmb.cn.gov.cn.mywmb.cn http://www.morning.drspc.cn.gov.cn.drspc.cn http://www.morning.dywgl.cn.gov.cn.dywgl.cn http://www.morning.qlckc.cn.gov.cn.qlckc.cn http://www.morning.kmwbq.cn.gov.cn.kmwbq.cn http://www.morning.jcpq.cn.gov.cn.jcpq.cn http://www.morning.djpzg.cn.gov.cn.djpzg.cn http://www.morning.lfpdc.cn.gov.cn.lfpdc.cn http://www.morning.kqpq.cn.gov.cn.kqpq.cn http://www.morning.lblsx.cn.gov.cn.lblsx.cn http://www.morning.gnkbf.cn.gov.cn.gnkbf.cn http://www.morning.rmfh.cn.gov.cn.rmfh.cn http://www.morning.pdtjj.cn.gov.cn.pdtjj.cn http://www.morning.qlrtd.cn.gov.cn.qlrtd.cn