Golang之函数练习
小例题:
package main import "fmt" /*
函数练习,
可变参数使用 写一个函数add 支持1个或多个int相加,并返回相加结果
写一个函数concat,支持1个或多个string拼接,并返回结果
*/
func add(a int, arg ...int) int {
sum := a
for i := ; i < len(arg); i++ {
sum += arg[i]
}
return sum
} func concat(a string, arg ...string) (result string) {
result = a
for i := ; i < len(arg); i++ {
result += arg[i]
}
return
} func main() {
sum := add(, , , , , )
fmt.Println(sum)
res:=concat("hello"," ","大屌")
fmt.Println(res)
}
九九乘法表:
package main import "fmt"
//99乘法表
func multi() {
for i := ; i < ; i++ {
for j := ; j <= i; j++ {
fmt.Printf("%d*%d=%d\t", (i + ), j+, (i+)*(j+))
}
fmt.Println()
} }
func main() {
multi() }
检测回文(中文):
package main import (
"fmt"
) func process(str string) bool {
t := []rune(str)
length := len(t)
for i, _ := range t {
if i == length/ {
break
}
last := length - i -
if t[i] != t[last] {
return false
}
}
return true
}
func main() {
var str string
fmt.Scanf("%sd", &str)
if process(str) {
fmt.Println("yes")
} else {
fmt.Println("no")
}
}
统计一段字符串,中文,字符,数字,空格,出现的次数:
package main import (
"bufio"
"fmt"
"os"
) func count(str string) (wordCount, spaceCount, numberCount, otherCount int) {
t := []rune(str)
for _, v := range t {
switch {
case v >= 'a' && v <= 'z':
fallthrough
case v >= 'A' && v <= 'Z':
//golang里面++是语句,不能写成表达式
wordCount++
case v == ' ':
spaceCount++
case v >= '' && v <= '':
numberCount++
default:
otherCount++
}
}
return
} func main() {
reader := bufio.NewReader(os.Stdin)
result, _, err := reader.ReadLine()
//如果错误不为空,说明有错,就报错
if err != nil {
fmt.Println("read from console err:", err)
return
}
wc,sc,nc,oc:=count(string(result))
fmt.Printf("word Count:%d\n space count:%d\n number count:%d\n others count:%d\n",wc,sc,nc,oc)
}
Golang之函数练习的更多相关文章
- golang的函数
在golang中, 函数是第一类值(first-class object), 即函数可以赋值与被赋值. 换言之, 函数也可以作为ReceiverType, 定义自己的method. 实例: http. ...
- golang(06)函数介绍
原文链接 http://www.limerence2017.com/2019/09/11/golang11/#more 函数简介 函数是编程语言中不可缺少的部分,在golang这门语言中函数是一等公民 ...
- golang笔记——函数与方法
如果你遇到没有函数体的函数声明,表示该函数不是以Go实现的. package math func Sin(x float64) float //implemented in assembly lang ...
- Golang tips ----- 函数
1.在函数调用时,Golang没有默认参数值 2.一个函数声明如果没有函数体,表面该函数不是由Golang实现的,这样的声明定义了函数标识符 3.拥有函数名的函数只能在包级语法块中被声明 4.函数值( ...
- golang 原子操作函数
golang中的原子操作在sync/atomic package中. 下文以比较和交换操作函数为例,介绍其使用. CompareAndSwapInt32 比较和交换操作是原子性的. // Compar ...
- [golang note] 函数定义
普通函数定义 √ golang函数基本组成:关键字func.函数名.参数列表.返回值.函数体和返回语句. • 语法如下 func 函数名(参数列表) (返回值列表) { // 函数体 } • 示例如下 ...
- golang中函数类型
今天看Martini文档,其功能列表提到完全兼容http.HandlerFunc接口,就去查阅了Go: net/http的文档,看到type HandlerFunc这部分,顿时蒙圈了.由于之前学习的时 ...
- golang:函数总结
golang保留的函数 init(), main()是golang的保留函数,有如下特点: main() 只能用在main包中,仅可定义一个,init() 可定义任意包,可重复定义,建议只定义一个 两 ...
- 【GoLang】函数作为 类型 和 值
代码示例 package test import ( "fmt" "testing" ) type testInt func(int) bool func is ...
随机推荐
- 把mac地址转换为标准mac地址
把"00:90:8A:1D:30:51"转换成"00-90-8A-1D-30-51",如何格式错误,显示出格式错误的种类,有些不规范的转换成规范的格式,例如,& ...
- springboot整合mybatis增删改查(三):mybatis逆向工程
上一篇已经把项目基本框架完善,接下来就是利用Mybatis Generator逆向工程进行mybatis的整合. 我们在创建项目开始的时候已经勾选web,mybatis,sql等,但是这些依赖还是不够 ...
- C-语言第二次作业(大一下)
要求一.设计过程 作业(1) 1.提交列表 6-7 删除字符串中数字字符 2.设计思路(6分 ...
- 6-17 Shortest Path [2](25 分)
Write a program to find the weighted shortest distances from any vertex to a given source vertex in ...
- USB学习笔记-总结
1. # ls /sys/bus/usb/devices/解析:1-0:1.0 1-1 1-1:1.0 2-0:1.0 2-1 2-1:1.0 2-2 2-2.1 2-2:1.0 2-2.1:1.0 ...
- Install LAMP Server (Apache, MariaDB, PHP) On CentOS/RHEL/Scientific Linux 7
Install LAMP Server (Apache, MariaDB, PHP) On CentOS/RHEL/Scientific Linux 7 By SK - August 12, 201 ...
- 错误 1 缺少编译器要求的成员“System.Runtime.CompilerServices.ExtensionAttrib
错误 1 缺少编译器要求的成员“System.Runtime.CompilerServices.ExtensionAttrib 删除Newtonsoft.Json.dll 引用 ,再重新引用即可. 原 ...
- thinkpad t420安装debian需要注意的细节
关闭双显卡,使用集成显卡,32位可以用独显跑起来,但是64位的wheezy只能兼容集成显卡,不知道为啥,否则会在某些usb插入/或者拔出来以后,重启提示 failed to execute /lib/ ...
- [转]iis 重新安装后 重新注册asp.net
iis 重新安装后 重新注册asp.net 服务器IIS问题: 卸载并重新安装了IIS.... 解决方法:原因是IIS重装后要重新安装一下.NET Framework. 开始-->运行--> ...
- javascript精髓篇之原型链维护和继承.
一.两个原型 很多人都知道javascript是原型继承,每个构造函数都有一个prototype成员,通过它就可以把javascript的继承演义的美轮美奂了. 其实啊,光靠这一个属性是无法完成jav ...