当前位置: 首页 > ds >正文

Go by Example

网页地址Go by Example 中文版

Github仓库地址mmcgrana/gobyexample:按示例进行

HelloWorld

package mainimport ("fmt"
)func main() {fmt.Println("Hello World")
}

Hello World 

package mainimport ("fmt"
)func main() {fmt.Println("go" + "lang")fmt.Println("1+1=", 1+1)fmt.Println("7.0/3.0", 7.0/3.0)fmt.Println(true && false)fmt.Println(true || false)fmt.Println(!true)
}

golang
1+1= 2
7.0/3.0 2.3333333333333335
false
true
false

变量

package mainimport "fmt"func main() {var a = "initial"fmt.Println(a)var b, c int = 1, 2fmt.Println(b, c)var d = truefmt.Println(d)var e intfmt.Println(e)f := "short"fmt.Println(f)
}

 initial
1 2
true
0
short

常量

package mainimport ("fmt""math"
)const s string = "constant"func main() {fmt.Println(s)const n = 500000000const d = 3e20 / nfmt.Println(d)fmt.Println(int64(d))fmt.Println(math.Sin(n))
}

 constant
6e+11
600000000000
-0.28470407323754404

For循环

package mainimport "fmt"func main() {i := 1for i <= 3 {fmt.Println(i)i++}for j := 7; j <= 9; j++ {fmt.Println(j)}for {fmt.Println("loop")break}for n := 0; n <= 5; n++ {if n%2 == 0 {continue}fmt.Println(n)}
}

1
2
3
7
8
9
loop
1
3

 If/Else分支

package mainimport "fmt"func main() {if 7%2 == 0 {fmt.Println("7 is even")} else {fmt.Println("7 is odd")}if 8%4 == 0 {fmt.Println("8 is divisible by 4")}if num := 9; num < 0 {fmt.Println(num, "is negative")} else if num < 10 {fmt.Println(num, "has 1 digit")} else {fmt.Println(num, "has multiple digits")}}

 7 is odd
8 is divisible by 4
9 has 1 digit

Switch分支结构

package mainimport ("fmt""time"
)func main() {i := 2fmt.Print("write ", i, " as ")switch i {case 1:fmt.Println("1")case 2:fmt.Println("2")case 3:fmt.Println("3")}switch time.Now().Weekday() {case time.Saturday, time.Sunday:fmt.Println("It's the weekend")default:fmt.Println("It's a weekday")}t := time.Now()switch {case t.Hour() < 12:fmt.Println("It's before noon")default:fmt.Println("It's after noon")}// 独立出来的类型判断函数whatAmI := func(i interface{}) {switch t := i.(type) {case bool:fmt.Println("I'm a bool")case int:fmt.Println("I'm an int")default:fmt.Printf("Don't know type %T\n", t)}}whatAmI(true)whatAmI(1)whatAmI("hey")
}

 write 2 as 2
It's the weekend
It's after noon
I'm a bool
I'm an int
Don't know type string

 数组

package mainimport "fmt"func main() {var a [5]intfmt.Println("emp:", a)a[4] = 100fmt.Println("set:", a)fmt.Println("get:", a[4])fmt.Println("len:", len(a))b := [5]int{1, 2, 3, 4, 5}fmt.Println("dcl:", b)var twoD [2][3]intfor i := 0; i < 2; i++ {for j := 0; j < 3; j++ {twoD[i][j] = i + j}}fmt.Println("2d: ", twoD)
}

 emp: [0 0 0 0 0]
set: [0 0 0 0 100]
get: 100
len: 5
dcl: [1 2 3 4 5]
2d:  [[0 1 2] [1 2 3]]

切片

package mainimport "fmt"func main() {s := make([]string, 3)fmt.Println("emp:", s)s[0] = "a"s[1] = "b"s[2] = "c"fmt.Println("set:", s)fmt.Println("get:", s[2])fmt.Println("len:", len(s))s = append(s, "d")s = append(s, "e", "f")fmt.Println("apd:", s)c := make([]string, len(s))copy(c, s)fmt.Println("cpy:", c)l := s[2:5]fmt.Println("sl1:", l)l = s[:5]fmt.Println("sl2:", l)l = s[2:]fmt.Println("sl3:", l)t := []string{"g", "h", "i"}fmt.Println("dcl:", t)twoD := make([][]int, 3)for i := 0; i < 3; i++ {innerLen := i + 1twoD[i] = make([]int, innerLen)for j := 0; j < innerLen; j++ {twoD[i][j] = i + j}}fmt.Println("2d: ", twoD)
}

 emp: [  ]
set: [a b c]
get: c
len: 3
apd: [a b c d e f]
cpy: [a b c d e f]
sl1: [c d e]
sl2: [a b c d e]
sl3: [c d e f]
dcl: [g h i]
2d:  [[0] [1 2] [2 3 4]]

 

 

 

http://www.xdnf.cn/news/16483.html

相关文章:

  • 深入解析命名管道:原理、实现与进程间通信应用
  • 深入React框架:构建现代前端应用的全面指南
  • Vue当中背景图无法占满屏幕的解决方法
  • 使用FRP搭建内网穿透工具,自己公网服务器独享内外网端口转发
  • Ubuntu 22.04 配置 Zsh + Oh My Zsh + Powerlevel10k
  • 物联网统一网关:多协议转换与数据处理架构设计
  • HiggsAudio-V2: 融合语言与声音的下一代音频大模型
  • 【企业架构】TOGAF概念之二
  • 数据结构(4)单链表算法题(上)
  • Linux库——库的制作和原理(2)_库的原理
  • c#抽象类和接口的异同
  • 八股文整理——计算机网络
  • Docker常用命令详解:以Nginx为例
  • 台式电脑有多个风扇开机只有部分转动的原因
  • 典型的 Vue 3 项目目录结构详解
  • 解决使用vscode连接服务器出现“正在下载 VS Code 服务器...”
  • 动态SQL标签
  • FROM stakater/java8-alpine 构建cocker镜像
  • 学习嵌入式的第三十三天-数据结构-(2025.7.25)服务器/多客户端模型
  • SSRF_XXE_RCE_反序列化学习
  • ChatIm项目文件上传与获取
  • 前缀和-238-除自身以外数组的乘积-力扣(LeetCode)
  • 《使用Qt Quick从零构建AI螺丝瑕疵检测系统》——6. 传统算法实战:用OpenCV测量螺丝尺寸
  • nginx一个域名下部署多套前端项目
  • GRE、MGRE实验
  • RK3568笔记九十三:基于RKNN Lite的YOLOv5目标检测
  • FreeMarker模板引擎
  • 【C++】C++11特性的介绍和使用(第三篇)
  • 【RHCSA 问答题】第 10 章 配置和保护 SSH
  • 航空发动机高速旋转件的非接触式信号传输系统