内容简介:猜数字练习
go流程控制
字符串详解
字符串原理
- 字符串底层就是一个byte数组,所以可以和[]byte类型互相转换
- 字符串中的字符是不能修改的
- 字符串是有byte字节组成,所以字符串的长度是byte字节的长度
- rune类型(int32占四个字节)用来表示utf8字符,一个rune由一个或者多个byte组成
func stringByte() {
var str string
str = "abc你好"
var b []byte = []byte(str)
var c[]rune = []rune(str)
fmt.Printf("%c\n",97)
fmt.Printf("b=%v,\tlen(str)=%d\n",b,len(str))
fmt.Printf("%d\n",len(c))
}
练习
写一个程序,对英文字符串进行逆序
func reverseStr(s string) string {
bytes := []byte(s)
for i:=0;i<len(s)/2;i++{
//fmt.Printf("%c",i) 输出字符串
var tmp = bytes[i]
bytes[i] = bytes[len(s)-i-1]
bytes[len(s)-i-1] = tmp
}
s = string(bytes)
return s
}
func main() {
//stringByte()
s:= "abcdefg"
fmt.Println(reverseStr(s))
}
写一个程序,实现对包含中文的字符串进行逆序
func reverseChinese(s string) {
bytes := []rune(s)
for i:=0;i< len(bytes)/2;i++ {
bytes[i],bytes[len(bytes)-i-1] = bytes[len(bytes)-i-1],bytes[i]
}
s = string(bytes)
fmt.Println(s)
}
func main() {
s:= "abcdefg中国"
fmt.Println(reverseChinese(s))
}
写一个程序,判断一个字符串是否是回文
func isReversed(s string) string {
r := []rune(s)
for i:=0;i <len(r)/2;i++ {
r[i],r[len(r)-i-1] = r[len(r)-i-1],r[i]
}
s1 := string(r)
if s1 == s{
return "is huiwen"
}else {
return "not huiwen"
}
}
日期和时间类型
- time包
- 获取当前时间
- 时间格式化
- 时间戳和时间之间的转换
- 定时器的简单使用
- time.Duration()表示纳秒
package main
import (
"fmt"
"time"
)
func getTime() {
now:=time.Now()
year := now.Year()
month := now.Month()
day := now.Day()
hour := now.Hour()
minute := now.Minute()
second := now.Second()
fmt.Printf("%v\n",now)
fmt.Printf("year=%d,month=%d,day=%d,hour=%d,minute=%d,second=%d\n",
year,month,day,hour,minute,second)
}
func getTimeStamp() {
now := time.Now()
timestamp := now.UnixNano()
fmt.Printf("%d\n",timestamp)
}
func Dingshiqi() {
timer :=time.NewTimer(time.Second)
for v := range timer.C {
fmt.Printf("time:%v\n",v)
//定时一秒刷新
timer.Reset(time.Second)
}
}
func TimeTicker() {
timer := time.NewTicker(time.Second)
for v := range timer.C {
fmt.Printf("time: %v\n",v)
}
}
func TimeStampToTime(timestamp int64) {
time_t := time.Unix(timestamp,0)
fmt.Printf("time %v\n",time_t)
}
func FormatTime() {
now := time.Now()
//Go 诞生时间
str := now.Format("2006/01/02 15:04:05")
fmt.Printf("str: %s\n",str)
}
func main() {
//getTime()
//getTimeStamp()
/*
//go Dingshiqi()
go TimeTicker()
time.Sleep(time.Minute)
*/
//TimeStampToTime(time.Now().Unix())
}
小练习
写一个程序,统计一段代码的执行耗时,单位精确到微秒
func testCase() {
for i:=0;i<10000000000;i++{
_=i
}
}
func main() {
//获取纳秒数
start_time := time.Now().UnixNano()
testCase()
end_time := time.Now().UnixNano()
fmt.Printf("cost time is %d us,also %d ms\n",(end_time-start_time)/1000,(end_time-start_time)/1000000)
}
流程控制
if条件语句
func judgeNum() {
num := 10
if num % 2 ==0 {
fmt.Printf("%d是偶数\n",num)
}else {
fmt.Printf("%d是奇数\n",num)
}
}
func guessNum() {
num := 99
if num <=50 {
fmt.Printf("%d小于等于50\n",num)
} else if num >= 51 && num <= 100 {
fmt.Printf("%d大于等于51,小于等于100\n",num)
} else {
fmt.Printf("%d大于100\n",num)
}
}
for循环
func simpleFor() {
for i:=1;i<=10;i++ {
fmt.Printf("%d\n",i)
}
}
func forcase() {
i := 0
for ;i<=10; {
fmt.Printf("%d\n",i)
i+=2
}
}
func breakFor() {
for i:=0;i<=10;i++ {
if i > 5 {
break
}
fmt.Printf("%d\n",i)
}
}
func continueFor() {
for i:=0;i<=10;i++ {
if i == 5 {
continue
}
fmt.Printf("%d\n",i)
}
}
func forCase() {
for no,i := 10,1;i<=10&&no<=19;i,no=i+1,no+1 {
fmt.Printf("%d * %d = %d\n",no,i,no*i)
}
}
// 无限循环
func deadLoop() {
for {
fmt.Printf("hello")
}
}
switch语句
func switchCase() {
f:=9
switch f {
case 1:
fmt.Printf("enter case 1\n")
fmt.Printf("f=1\n")
//fallthrough
case 2:
fmt.Printf("enter case 2\n")
fmt.Printf("f=2\n")
case 3:
fmt.Printf("enter case 3\n")
fmt.Printf("f=3\n")
default:
fmt.Printf("enter default case\n")
}
}
func switchCase2 () {
letter := "i"
switch letter {
case "a","e","i","o","u":
fmt.Printf("元音字母%s\n",letter)
default:
fmt.Printf("辅音字母%s\n",letter)
}
}
//条件判断格式
func switchCase3() {
num := 75
switch {
case num<75:
fmt.Printf("数字 %d 小于75\n",num)
case num>=75 && num <=85:
fmt.Printf("数字%d大于75,小于85\n",num)
default:
fmt.Printf("数字%d大于85\n",num)
}
}
小练习
猜数字练习
func randCase() {
var number int
/*
伪随机数
for i:=0;i<10;i++{
number = rand.Intn(100)
fmt.Printf("number:%d\n",number)
}
return
*/
//生成随机数种子
rand.Seed(time.Now().UnixNano())
number = rand.Intn(100)
fmt.Println(number)
fmt.Printf("猜一个数字,数字的范围是0到100\n")
for {
var input int
fmt.Scanf("%d\n",&input)
var flag bool = false
switch {
case number > input:
fmt.Printf("输入的比较小%d\n",input)
case number == input:
fmt.Printf("猜对了%d,正确值是%d\n",input,number)
flag = true
case number<input:
fmt.Printf("输入的比较大%d\n",input)
}
if flag {
break
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 数据管理流程,基础入门简介
- 兄弟连区块链入门教程以太坊源码分析geth启动流程分析
- OpenGL ES 入门之旅--OpenGL 下的坐标系和着色器渲染流程
- 【轻知识】Go入门学习整理——第四节web开发,http请求处理流程、一个简单的名单录入。
- 【Tomcat学习笔记】启动流程分析--总体流程
- 【Tomcat学习笔记】启动流程分析--总体流程
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Agile Web Application Development with Yii 1.1 and PHP5
Jeffrey Winesett / Packt Publishing / 2010-08-27
In order to understand the framework in the context of a real-world application, we need to build something that will more closely resemble the types of applications web developers actually have to bu......一起来看看 《Agile Web Application Development with Yii 1.1 and PHP5》 这本书的介绍吧!