8-1 http标准库

1635335363928

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/http/httputil"
  6. )
  7. func main() {
  8. resp, err := http.Get("http://www.imooc.com")
  9. if err != nil {
  10. panic(err)
  11. }
  12. defer resp.Body.Close()
  13. response, err := httputil.DumpResponse(resp, true)
  14. if err != nil {
  15. panic(err)
  16. }
  17. fmt.Printf("%s\n", response)
  18. }

我现在想对 httpclinet进行控制

比如我想要访问手机版的imooc

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/http/httputil"
  6. )
  7. func main() {
  8. request, err := http.NewRequest(
  9. http.MethodGet,
  10. "http://www.imooc.com",
  11. nil,
  12. )
  13. request.Header.Add("User-Agent","Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1")
  14. resp, err := http.DefaultClient.Do(request)
  15. if err != nil {
  16. panic(err)
  17. }
  18. defer resp.Body.Close()
  19. response, err := httputil.DumpResponse(resp, true)
  20. if err != nil {
  21. panic(err)
  22. }
  23. fmt.Printf("%s\n", response)
  24. }

1635336316220
1635336328401

8-2 json数据格式的处理

+v格式化打印

  1. package main
  2. import "fmt"
  3. type Order struct {
  4. Id string
  5. Name string
  6. Quantity int
  7. TotalPrice int
  8. }
  9. func main() {
  10. o:=Order{
  11. Id: "124",
  12. Name: "lreag go",
  13. Quantity: 3,
  14. TotalPrice: 30,
  15. }
  16. fmt.Printf("%+v",o)
  17. }

使用 json库 格式化库

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Order struct {
  7. Id string
  8. Name string
  9. Quantity int
  10. TotalPrice int
  11. }
  12. func main() {
  13. o:=Order{
  14. Id: "124",
  15. Name: "lreag go",
  16. Quantity: 3,
  17. TotalPrice: 30,
  18. }
  19. //fmt.Printf("%+v",o)
  20. b,err:=json.Marshal(o)
  21. if err != nil {
  22. panic(err)
  23. }
  24. fmt.Printf("%s\n",b)
  25. }

json字段

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Order struct {
  7. Id string `json:"id"`
  8. Name string `json:"name"`
  9. Quantity int `json:"quantity"`
  10. TotalPrice int`json:"total_price"`
  11. }
  12. func main() {
  13. o:=Order{
  14. Id: "124",
  15. Name: "lreag go",
  16. Quantity: 3,
  17. TotalPrice: 30,
  18. }
  19. //fmt.Printf("%+v",o)
  20. b,err:=json.Marshal(o)
  21. if err != nil {
  22. panic(err)
  23. }
  24. fmt.Printf("%s\n",b)
  25. }

在结构体里面,首字母小写的字段是不能被看到的

省略空字段

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Order struct {
  7. Id string `json:"id"`
  8. Name string `json:"name,omitempty"`
  9. Quantity int `json:"quantity"`
  10. TotalPrice int `json:"total_price"`
  11. }
  12. func main() {
  13. o := Order{
  14. Id: "124",
  15. //Name: "lreag go",
  16. Quantity: 3,
  17. TotalPrice: 30,
  18. }
  19. //fmt.Printf("%+v",o)
  20. b, err := json.Marshal(o)
  21. if err != nil {
  22. panic(err)
  23. }
  24. fmt.Printf("%s\n", b)
  25. }
  26. /**
  27. {"id":"124","quantity":3,"total_price":30}
  28. Process finished with the exit code 0
  29. */

json 嵌套

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type OrderItem struct {
  7. ID string `json:"id"`
  8. Name string `json:"name"`
  9. Price float64 `json:"price"`
  10. }
  11. type Order struct {
  12. Id string `json:"id"`
  13. Item OrderItem `json:"item"`
  14. Quantity int `json:"quantity"`
  15. TotalPrice int `json:"total_price"`
  16. }
  17. func main() {
  18. o := Order{
  19. Id: "124",
  20. Item: OrderItem{
  21. ID: "8675645",
  22. Name: "nancy",
  23. Price: 0,
  24. },
  25. Quantity: 3,
  26. TotalPrice: 30,
  27. }
  28. //fmt.Printf("%+v",o)
  29. b, err := json.Marshal(o)
  30. if err != nil {
  31. panic(err)
  32. }
  33. fmt.Printf("%s\n", b)
  34. }
  35. /**
  36. {
  37. "id": "124",
  38. "item": {
  39. "id": "8675645",
  40. "name": "nancy",
  41. "price": 0
  42. },
  43. "quantity": 3,
  44. "total_price": 30
  45. }
  46. */

指针也可以

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type OrderItem struct {
  7. ID string `json:"id"`
  8. Name string `json:"name"`
  9. Price float64 `json:"price"`
  10. }
  11. type Order struct {
  12. Id string `json:"id"`
  13. Item *OrderItem `json:"item"`
  14. Quantity int `json:"quantity"`
  15. TotalPrice int `json:"total_price"`
  16. }
  17. func main() {
  18. o := Order{
  19. Id: "124",
  20. Item: &OrderItem{
  21. ID: "8675645",
  22. Name: "nancy",
  23. Price: 0,
  24. },
  25. Quantity: 3,
  26. TotalPrice: 30,
  27. }
  28. //fmt.Printf("%+v",o)
  29. b, err := json.Marshal(o)
  30. if err != nil {
  31. panic(err)
  32. }
  33. fmt.Printf("%s\n", b)
  34. }
  35. /**
  36. {
  37. "id": "124",
  38. "item": {
  39. "id": "8675645",
  40. "name": "nancy",
  41. "price": 0
  42. },
  43. "quantity": 3,
  44. "total_price": 30
  45. }
  46. */

切片也支持

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type OrderItem struct {
  7. ID string `json:"id"`
  8. Name string `json:"name"`
  9. Price float64 `json:"price"`
  10. }
  11. type Order struct {
  12. Id string `json:"id"`
  13. Item *[]OrderItem `json:"item"`
  14. Quantity int `json:"quantity"`
  15. TotalPrice int `json:"total_price"`
  16. }
  17. func main() {
  18. o := Order{
  19. Id: "124",
  20. Item: &[]OrderItem{
  21. {
  22. ID: "8675645",
  23. Name: "nancy",
  24. Price: 50,
  25. },
  26. {
  27. ID: "2456733",
  28. Name: "yael",
  29. Price: 20,
  30. },
  31. },
  32. Quantity: 3,
  33. TotalPrice: 30,
  34. }
  35. //fmt.Printf("%+v",o)
  36. b, err := json.Marshal(o)
  37. if err != nil {
  38. panic(err)
  39. }
  40. fmt.Printf("%s\n", b)
  41. }
  42. /**
  43. {
  44. "id": "124",
  45. "item": [
  46. {
  47. "id": "8675645",
  48. "name": "nancy",
  49. "price": 50
  50. },
  51. {
  52. "id": "2456733",
  53. "name": "yael",
  54. "price": 20
  55. }
  56. ],
  57. "quantity": 3,
  58. "total_price": 30
  59. }
  60. */

1635345615051

8-3 第三方API数据格式的解析技巧

map类型收 json

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type OrderItem struct {
  7. ID string `json:"id"`
  8. Name string `json:"name"`
  9. Price float64 `json:"price"`
  10. }
  11. type Order struct {
  12. Id string `json:"id"`
  13. Item []*OrderItem `json:"item"`
  14. Quantity int `json:"quantity"`
  15. TotalPrice int `json:"total_price"`
  16. }
  17. func marshal() {
  18. o := Order{
  19. Id: "124",
  20. Item: []*OrderItem{
  21. {
  22. ID: "8675645",
  23. Name: "nancy",
  24. Price: 50,
  25. },
  26. {
  27. ID: "2456733",
  28. Name: "yael",
  29. Price: 20,
  30. },
  31. },
  32. Quantity: 3,
  33. TotalPrice: 30,
  34. }
  35. fmt.Println(o)
  36. }
  37. func unmarshal() {
  38. s := `{"id":"124","item":[{"id":"8675645","name":"nancy","price":50},{"id":"2456733","name":"yael","price":20}],"quantity":3,"total_price":30}
  39. `
  40. var o Order
  41. json.Unmarshal([]byte(s), &o)
  42. fmt.Printf("%+v", o)
  43. }
  44. func parseNLP() {
  45. res := `{
  46. "data": [
  47. {
  48. "synonym":"",
  49. "weight":"0.6",
  50. "word": "真丝",
  51. "tag":"材质"
  52. },
  53. {
  54. "synonym":"",
  55. "weight":"0.8",
  56. "word": "韩都衣舍",
  57. "tag":"品牌"
  58. },
  59. {
  60. "synonym":"连身裙;联衣裙",
  61. "weight":"1.0",
  62. "word": "连衣裙",
  63. "tag":"品类"
  64. }
  65. ]
  66. }`
  67. //m := struct {
  68. // Data []struct {
  69. // Synonym string `json:"synonym"`
  70. // Tag string `json:"tag"`
  71. // } `json:"data"`
  72. //}{}
  73. m := make(map[string]interface{})
  74. err := json.Unmarshal([]byte(res), &m)
  75. if err != nil {
  76. panic(err)
  77. }
  78. fmt.Printf("-----------------------------------------------------------\n")
  79. fmt.Printf("%+v\n",m["data"].([]interface{})[2].(map[string]interface{})["synonym"])
  80. fmt.Printf("-----------------------------------------------------------\n")
  81. for k,v := range m{
  82. fmt.Println(k,v)
  83. for _,v2 := range v.([]interface{}){
  84. for v3,v4:=range v2.(map[string]interface{}){
  85. fmt.Printf("v3:%v---v4:%v\n",v3,v4)
  86. }
  87. //fmt.Printf("v1:%v---v2%v\n",v1,v2)
  88. }
  89. }
  90. fmt.Printf("%+v\n", m)
  91. //fmt.Printf("%+v, %+v\n", m.Data[2].Synonym, m.Data[2].Tag)
  92. }
  93. func main() {
  94. parseNLP()
  95. }

其中的map[string]interface 属实复杂

  1. fmt.Printf("%+v\n",
  2. m["data"].([]interface{})[2].(map[string]interface{})["synonym"],
  3. )

结构体收json

换成结构体的

  1. m := struct {
  2. Data []struct {
  3. Synonym string `json:"synonym"`
  4. Tag string `json:"tag"`
  5. } `json:"data"`
  6. }{}
  1. err := json.Unmarshal([]byte(res), &m)
  2. if err != nil {
  3. panic(err)
  4. }
  5. fmt.Printf("%+v, %+v\n", m.Data[2].Synonym, m.Data[2].Tag)

8-4 gin框架介绍

8-5 为gin增加middleware