0、下载 Windows 安装包
官方地址在这儿:https://golang.google.cn/
下载包地址:https://golang.google.cn/dl/go1.16.4.windows-amd64.msi

1、安装 go1.16.4.windows-amd64.msi
点击 "Next" 就行2021-05-29_171051.png
图1、安装Go语言开发程序过程图

2、验证运行go version
在命令行(cmd)中,运行 go version 出现

go version go1.64.4 windows/amd64

就表示安装成功了。

3、编写运行 Hello World
在命令行下面执行相应操作就好

mkdir hellowin10
cd hellowin10
go mod init const.net.cn/hellowin10
notepad hello.go

hello.go内容如下:

package main
import (
"fmt"
)
func main(){
 fmt.Println("Hello Win10")
}

运行

go run .

结果

Hello Win10

1、使用 os/exec 中的exec.Command方法来实现

package main
import (
"fmt"
"os/exec"
)
func main(){
 cmd := exec.Command("ipconfig", "/all")
 output, err := cmd.CombinedOutput()
 if err != nil {
 fmt.Println(fmt.Sprint(err) + ":" + string(output))
 }
 fmt.Println(string(output))
}

运行
go run .
结果... 乱码
2021-05-29_173706.png

2、解决 Go exec.Command 乱码
这个问题主要应该还是在Windows下出现,Linux下工具没有提示中文信息,所以没有遇到这个问题
如果命令执行结果输出的是英文提示信息,在 命令行提示下 输入

chcp 65001

应该就可以正常了。
2021-05-29_174313.png

3、解决 Go exec.Command 中文乱码
中文乱码的问题没什么好方案了,只能老老实实转码了。
需要引入一个新的mod

go get -v  golang.org/x/text/encoding/simplifiedchinese 

提示这个才叫正确
go: downloading golang.org/x/text v0.3.6
go: found golang.org/x/text/encoding/simplifiedchinese in golang.org/x/text v0.3.6
golang.org/x/text/encoding/internal/identifier
golang.org/x/text/transform
golang.org/x/text/encoding
golang.org/x/text/encoding/internal
golang.org/x/text/encoding/simplifiedchinese

如果提示类似错误信息

go get golang.org/x/text/encoding/simplifiedchinese: module golang.org/x/text/encoding/simplifiedchinese: Get "https://proxy.golang.org/golang.org/x/text/encoding/simplifiedchinese/@v/list": dial tcp 172.217.160.81:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
就加个http_proxy吧

set http_proxy=http://localhost:1080

完整的代码

package main
import (
 "fmt"
 "os/exec"
 "golang.org/x/text/encoding/simplifiedchinese"
)

type Charset string

const (
    UTF8    = Charset("UTF-8")
    GB18030 = Charset("GB18030")
)

func main(){
 cmd := exec.Command("ipconfig", "/all")
 output, err := cmd.CombinedOutput()
 if err != nil {
 fmt.Println(fmt.Sprint(err) + ":" + string(output))
 }
 cmdRe:=ConvertByte2String([]byte(string(output)),"GB18030")
 fmt.Println(cmdRe)    
}

func ConvertByte2String(byte []byte, charset Charset) string {
    var str string
    switch charset {
    case GB18030:
        var decodeBytes,_=simplifiedchinese.GB18030.NewDecoder().Bytes(byte)
        str= string(decodeBytes)
    case UTF8:
        fallthrough
    default:
        str = string(byte)
    }
    return str
}

运行结果:
2021-05-29_180125.png

1、string 转为[]byte

var str string = "const.net.cn"
var data []byte = []byte(str)

2、byte转为string

var data [5]byte 
byte[0] = 'c'
byte[1] = 'o'
byte[2] = 'n'
byte[3] = 's'
byte[4] = 't'
var str string = string(data[:])

1. Go run shell script or Go run shell string

curl := "curl -v https://const.net.cn"
cmd := exec.Command("bash", "-c", curl)
stdout, err := cmd.CombinedOutput()
if err != nil {
    fmt.Println("curl " + err.Error())
    return
}
fmt.Println(string(stdout)) 

2.输出结果

  • Uses proxy env variable https_proxy == 'socks5://127.0.0.1:1080'
  • Trying 127.0.0.1:1080...
  • TCP_NODELAY set % Total % Received % Xferd Average Speed Time Time Time Current

                                 Dload  Upload   Total   Spent    Left  Speed   0     0    0     0    0     0      0      0 --:--:-- --:--:--

    --:--:-- 0

  • SOCKS5 communication to const.net.cn:443
  • SOCKS5 connect to IPv4 43.129.233.128:443 (locally resolved)
  • SOCKS5 request granted.
  • Connected to 127.0.0.1 (127.0.0.1) port 1080 (#0)
  • ALPN, offering h2
  • ALPN, offering http/1.1
  • successfully set certificate verify locations:
  • CAfile: /etc/ssl/certs/ca-certificates.crt CApath: /etc/ssl/certs } [5 bytes data]
  • TLSv1.3 (OUT), TLS handshake, Client hello (1): } [512 bytes data]
  • TLSv1.3 (IN), TLS handshake, Server hello (2): { [122 bytes data]
  • TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8): { [25 bytes data]
  • TLSv1.3 (IN), TLS handshake, Certificate (11): { [2780 bytes data]
  • TLSv1.3 (IN), TLS handshake, CERT verify (15): { [264 bytes data]
  • TLSv1.3 (IN), TLS handshake, Finished (20): { [52 bytes data]
  • TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1): } [1 bytes data]
  • TLSv1.3 (OUT), TLS handshake, Finished (20): } [52 bytes data]
  • SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
  • ALPN, server accepted to use http/1.1
  • Server certificate:
  • subject: CN=const.net.cn
  • start date: May 19 00:00:00 2021 GMT
  • expire date: May 18 23:59:59 2022 GMT
  • subjectAltName: host "const.net.cn" matched cert's "const.net.cn"
  • issuer: C=CN; O=TrustAsia Technologies, Inc.; OU=Domain Validated SSL; CN=TrustAsia TLS RSA CA
  • SSL certificate verify ok. } [5 bytes data]

GET / HTTP/1.1
Host: const.net.cn
User-Agent: curl/7.68.0
Accept: /

......

0、curl 显示连接的常用方法

curl -d "hello=world" https://const.net.cn/test/ --verbose

输出结果:
...

  • Server certificate:
  • subject: CN=const.net.cn
  • start date: May 19 00:00:00 2021 GMT
  • expire date: May 18 23:59:59 2022 GMT
  • subjectAltName: host "const.net.cn" matched cert's "const.net.cn"
  • issuer: C=CN; O=TrustAsia Technologies, Inc.; OU=Domain Validated SSL; CN=TrustAsia TLS RSA CA
  • SSL certificate verify ok.

POST /test/ HTTP/1.1
Host: const.net.cn
User-Agent: curl/7.68.0
Accept: /
Content-Length: 11
Content-Type: application/x-www-form-urlencoded

  • upload completely sent off: 11 out of 11 bytes
    ...

1、curl 显示 form 数据

curl -d "hello=world" https://const.net.cn/test/ --trace-ascii /dev/stdout

输出结果:
0000: POST /test/ HTTP/1.1
0016: Host: const.net.cn
002a: User-Agent: curl/7.68.0
0043: Accept: /
0050: Content-Length: 11
0064: Content-Type: application/x-www-form-urlencoded
0095:
=> Send data, 11 bytes (0xb)
0000: hello=world
== Info: upload completely sent off: 11 out of 11 bytes

2、curl 显示 boundary 数据

curl -F "hello=world" -F file=@/etc/timezone https://const.net.cn/test/ --trace-ascii /dev/stdout

输出结果:

0000: POST /test/ HTTP/1.1 0016: Host: const.net.cn 002a: User-Agent:
curl/7.68.0 0043: Accept: / 0050: Content-Length: 313 0065:
Content-Type: multipart/form-data; boundary=-------------------- 00a5:
----584e7b2299bf345b 00bb:
=> Send SSL data, 5 bytes (0x5) 0000: ....J
=> Send SSL data, 1 bytes (0x1) 0000: .
=> Send data, 313 bytes (0x139) 0000: --------------------------584e7b2299bf345b 002c: Content-Disposition: form-data; name="hello" 005a: 005c: world 0063:
--------------------------584e7b2299bf345b 008f: Content-Disposition: form-data; name="file"; filename="timezone" 00d1: Content-Type:
application/octet-stream 00f9: 00fb: Asia/Shanghai. 010b:
--------------------------584e7b2299bf345b--
== Info: We are completely uploaded and fine