Go语言的调试

日志:

Go语言提高了log包,让应用程序能够将日志写入终端或文件。

Go语言提供的日志功能,程序清单如下:

package main

import(
  "log"
)

func main(){
  log.Printf("This is a log message.")
}

运行结果如下:

2022/01/25 19:10:06 This is a log message.

记录致命错误,程序清单如下:

package main

import(
  "errors"
  "log"
)

func main(){
  var errFatal=errors.New("We only just started and we are crashing")
  log.Fatal(errFatal)
}

运行结果如下:

2022/01/25 19:13:57 We only just started and we are crashing
exit status 1

将日志写入文件:程序清单如下:

package main

import(
  "log"
  "os"
)

func main(){
  f,err:=os.OpenFile("main.txt",os.O_APPEND|os.O_CREATE|os.O_RDWR,0666)
  if err!=nil{
    log.Fatal(err)
  }
  defer f.Close()
  log.SetOutput(f)
  for i:=1;i<=5;i++{
    log.Printf("Log iteration %d",i)
  }
}

运行之后,生成main.txt新文件,打开main.txt文件内容如下:

2022/01/25 19:24:32 Log iteration 1
2022/01/25 19:24:32 Log iteration 2
2022/01/25 19:24:32 Log iteration 3
2022/01/25 19:24:32 Log iteration 4
2022/01/25 19:24:32 Log iteration 5

日志程序示例,程序清单如下:

package main

import(
  "log"
)

func main(){
  for i:=1;i<=5;i++{
    log.Printf("Log iteration %d",i)
  }
}

输入命令行如下:

go run main.go>main1.txt 2>&1

运行之后,生成main1.txt新文件,打开main1.txt文件内容如下:

2022/01/25 19:32:04 Log iteration 1
2022/01/25 19:32:04 Log iteration 2
2022/01/25 19:32:04 Log iteration 3
2022/01/25 19:32:04 Log iteration 4
2022/01/25 19:32:04 Log iteration 5

打印数据:

简单的猜名游戏,程序清单如下:

package main

import(
  "bufio"
  "fmt"
  "os"
  "strings"
)

func main(){
  reader:=bufio.NewReader(os.Stdin)
  fmt.Print("Guess the name of my pet to win a prize: ")
  text,_:=reader.ReadString('\n')
  text=strings.Replace(text,"\n","",-1)
  fmt.Println("[DEBUG] text is:",text)
  if text=="John"{
    fmt.Println("You won! You win chocolate!")
  }else{
    fmt.Println("You didn't win. Better luck next time")
  }
}

运行结果如下:输入john

Guess the name of my pet to win a prize: john
[DEBUG] text is: john
You didn't win. Better luck next time

再次运行结果如下:输入John

Guess the name of my pet to win a prize: John
[DEBUG] text is: John
You won! You win chocolate!

使用fmt包:

使用fmt包来调试代码,程序清单如下:

package main

import(
  "fmt"
)

func main(){
  s:="Hello World"
  fmt.Printf("String is %v\n",s)
}

运行结果如下:

String is Hello World

使用fmt包打印多个变量,程序清单如下:

package main

import(
  "fmt"
)

func main(){
  s:="Hello World"
  t:="Goodbye, Cruel World"
  fmt.Printf("s is %v, t is %v\n",s,t)
}

运行结果如下:

s is Hello World, t is Goodbye, Cruel World

使用fmt包打印结构体的值,程序清单如下:

package main

import(
  "fmt"
)
type Animal struct{
  Name string
  Color string
}
func main(){
  a:=Animal{
    Name:"cat",
    Color:"Black",
  }
  fmt.Printf("%v\n",a)
  fmt.Printf("%+v\n",a)
}

运行结果如下:

{cat Black}
{Name:cat Color:Black}

使用Delve:

Go语言没有官方调试器,但很多社区项目都提供了Go语言调试器。

要安装delve,在终端上输入命令行如下:

go get github.com/derekparker/delve/cmd/dlv

运行结果如下:

# cd .; git clone -- https://github.com/derekparker/delve /Users/douxiaobo/go/src/github.com/derekparker/delve

Cloning into '/Users/douxiaobo/go/src/github.com/derekparker/delve'...

fatal: unable to access 'https://github.com/derekparker/delve/': LibreSSL SSL_connect: Operation timed out in connection to github.com:443

package github.com/derekparker/delve/cmd/dlv: exit status 128

要检查是否正确地安装了Delve,在终端中执行命令dlv --help。

运行结果如下:

-bash: dlv: command not found

这个问题就搁置了。

使用gdb:

根据上面的代码,在终端输入命令行如下:

$ go build main.go

再输入命令行如下:

gdb main

结果如下:

-bash: gdb: command not found

有可能在Mac os 没有GUN调试器。

输入命令行如下:

break main.echo

运行结果如下:

-bash: break: only meaningful in a `for', `while', or `until' loop

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值