此篇文章是对 《34 --> OpenWRT 嵌入式mips环境的go 环境搭建、gin验证测试代码分享》文章的补充,因为测试的时候,发现一个关于 protobuf 库依赖错误问题,go wiki 上说 go1.11 版本就存在此问题,所以此篇属于填补上一篇承诺分享 gin框架和源码诺言的实现。

第一步 下载 go1.15.14

1.1 编译go交叉编译环境

curl -# -O https://dl.google.com/go/go1.15.14.src.tar.gz

tar -zxvf go1.11.src.tar.gz 

cd go1.15.14/src


robot@ubuntu:~/golang/go1.15.14/src$ CGO_ENABLED=1 GOOS=linux GOARCH=mipsle GOMIPS=softfloat ./make.bash
2021/07/24 15:26:52 unsupported GOARCH mipsle
Building Go cmd/dist using /home/robot/golang/go1.4. ()
Building Go toolchain1 using /home/robot/golang/go1.4.
Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
Building Go toolchain2 using go_bootstrap and Go toolchain1.
Building Go toolchain3 using go_bootstrap and Go toolchain2.
Building packages and commands for host, linux/amd64.
Building packages and commands for target, linux/mipsle.
---
Installed Go for linux/mipsle in /home/robot/golang/go1.15.14
Installed commands in /home/robot/golang/go1.15.14/bin

1.2 配置开发环境

export CC_FOR_TARGET=/opt/ixecloud-toolchain-0.11-ramips-mt7621_gcc-7.5.0_musl.Linux-x86_64/toolchain-mipsel_24kc_gcc-7.5.0_musl/bin/mipsel-openwrt-linux-gcc
export CXX_FOR_TARGET=/opt/ixecloud-toolchain-0.11-ramips-mt7621_gcc-7.5.0_musl.Linux-x86_64/toolchain-mipsel_24kc_gcc-7.5.0_musl/bin/mipsel-openwrt-linux-g++
export STAGING_DIR=/home/robot/OpenWrt/mtk7621-19.07/staging_dir

export GOROOT=/home/robot/golang/go1.15.14
export GOBIN=$GOROOT/bin
export GOPATH=/home/robot/golang/workspace1.15
export PATH=$PATH:$GOBIN:GOPATH/bin

第二步 编写gin的例程

package main

import (
        "log"
        "github.com/gin-gonic/gin"
        "net/http"
)

func main() {
    r := gin.Default()
    r.GET("/welcome",func(c *gin.Context){
        json := make(map[string]interface{})
        c.BindJSON(&json)
        log.Printf("%v",&json)
        c.JSON(http.StatusOK, gin.H{
            "name":json["name"],
            "password":json["password"],
        })
    })

第三步 安装go依赖包&编译例程

// 安装源码中的gin框架的依赖包
robot@ubuntu:~/golang/workspace1.15$ go get github.com/gin-gonic/gin
// 编译 & 并运行 hello.go
robot@ubuntu:~/golang/workspace1.15$ go build hello.go 
robot@ubuntu:~/golang/workspace1.15$ go run hello.go 
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:	export GIN_MODE=release
 - using code:	gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET    /welcome                  --> main.main.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8088

第四步 使用 postman软件测试

在这里插入图片描述
下面是运行日志信息


[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 200
[GIN] 2021/07/24 - 16:16:24 | 200 |     125.007µs |   192.168.90.29 | GET      "/welcome"
2021/07/24 16:17:16 &map[name:lena password:123] //参数内容
[GIN] 2021/07/24 - 16:17:16 | 200 |     318.227µs |   192.168.90.29 | GET      "/welcome"

在嵌入式系统上测试效果一致;编译后的源码size有点大,可以采用上篇分享文章中的方法,减少size。至此,自己就把承诺给兑现了,谢谢大家。

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐