基本介绍

gutil 组件封装了一些开发中常用的工具方法。

使用方式:

  1. import "github.com/gogf/gf/v2/util/gutil"

接口文档:

https://pkg.go.dev/github.com/gogf/gf/v2/util/gutil

常用方法

Dump

  • 说明: Dumpvalues 以更好的可读性的方式输出在标准输出中。

  • 格式:

  1. Dump(values ...interface{})
  • 示例:
  1. type User struct {
  2. Name string
  3. Age int
  4. }
  5. type Location struct {
  6. Province string
  7. City string
  8. }
  9. type UserInfo struct {
  10. U User
  11. L Location
  12. }
  13. func main() {
  14. userList := make([]UserInfo, 0)
  15. userList = append(userList, UserInfo{
  16. U: User{
  17. Name: "郭强",
  18. Age: 18,
  19. },
  20. L: Location{
  21. Province: "四川",
  22. City: "成都",
  23. },
  24. })
  25. userList = append(userList, UserInfo{
  26. U: User{
  27. Name: "黄骞",
  28. Age: 18,
  29. },
  30. L: Location{
  31. Province: "江苏",
  32. City: "南京",
  33. },
  34. })
  35. gutil.Dump(userList)
  36. }
  37. // Output:
  38. [
  39. {
  40. U: {
  41. Name: "郭强",
  42. Age: 18,
  43. },
  44. L: {
  45. Province: "四川",
  46. City: "成都",
  47. },
  48. },
  49. {
  50. U: {
  51. Name: "黄骞",
  52. Age: 18,
  53. },
  54. L: {
  55. Province: "江苏",
  56. City: "南京",
  57. },
  58. },
  59. ]

DumpWithType

  • 说明: DumpWithTypeDump 类似,但是多了类型信息。

  • 格式:

  1. DumpWithType(values ...interface{})
  • 示例:
  1. type User struct {
  2. Name string
  3. Age int
  4. }
  5. type Location struct {
  6. Province string
  7. City string
  8. }
  9. type UserInfo struct {
  10. U User
  11. L Location
  12. }
  13. func main() {
  14. userList := make([]UserInfo, 0)
  15. userList = append(userList, UserInfo{
  16. U: User{
  17. Name: "郭强",
  18. Age: 18,
  19. },
  20. L: Location{
  21. Province: "四川",
  22. City: "成都",
  23. },
  24. })
  25. userList = append(userList, UserInfo{
  26. U: User{
  27. Name: "黄骞",
  28. Age: 18,
  29. },
  30. L: Location{
  31. Province: "江苏",
  32. City: "南京",
  33. },
  34. })
  35. gutil.DumpWithType(userList)
  36. }
  37. // Output:
  38. []main.UserInfo(2) [
  39. main.UserInfo(2) {
  40. U: main.User(2) {
  41. Name: string(6) "郭强",
  42. Age: int(18),
  43. },
  44. L: main.Location(2) {
  45. Province: string(6) "四川",
  46. City: string(6) "成都",
  47. },
  48. },
  49. main.UserInfo(2) {
  50. U: main.User(2) {
  51. Name: string(6) "黄骞",
  52. Age: int(18),
  53. },
  54. L: main.Location(2) {
  55. Province: string(6) "江苏",
  56. City: string(6) "南京",
  57. },
  58. },
  59. ]

DumpTo

  • 说明: DumpTovalue 以自定义的输出形式写入到 write 中。

  • 格式:

  1. DumpTo(writer io.Writer, value interface{}, option DumpOption)
  • 示例:
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/gogf/gf/v2/util/gutil"
  6. "io"
  7. )
  8. type UserInfo struct {
  9. Name string
  10. Age int
  11. Province string
  12. City string
  13. }
  14. type DumpWriter struct {
  15. Content string
  16. }
  17. func (d *DumpWriter) Write(p []byte) (n int, err error) {
  18. buffer := bytes.NewBuffer(nil)
  19. buffer.WriteString("I'm Start!\n")
  20. buffer.WriteString(string(p))
  21. buffer.WriteString("\nI'm End!\n")
  22. d.Content = buffer.String()
  23. return buffer.Len(), nil
  24. }
  25. func main() {
  26. u := UserInfo{
  27. "a", 18, "b", "c",
  28. }
  29. var dw io.Writer = &DumpWriter{}
  30. gutil.DumpTo(dw, u, gutil.DumpOption{})
  31. fmt.Println(dw.(*DumpWriter).Content)
  32. }
  33. // Output:
  34. I'm Start!
  35. {
  36. Name: "a",
  37. Age: 18,
  38. Province: "b",
  39. City: "c",
  40. }
  41. I'm End!