golang 开发入门教程,顺便写了个公众号批量下载工具
国庆节7天假期学习了下golang开发,顺便写了个公众号批量下载工具,分享下学习路径。
搭建环境
下载对应系统的安装包https://go.dev/dl/ ,如果下载慢可以用镜像https://mirrors.aliyun.com/golang/, https://golang.google.cn/dl/ ,http://mirrors.ustc.edu.cn/golang/ ,https://studygolang.com/dl ,比如Windows系统下载这个https://studygolang.com/dl/golang/go1.19.1.windows-amd64.msi ,如果电脑里已经有go,安装之前会提示卸载旧版本,确定后直接覆盖安装在原来的Go目录。
新版本的gopath 默认在用户目录下C:\Users\xxx\go,go module是go官方自带的go依赖管理库,所有依赖在 go.mod 文件,类似php的composer.json,而且提供了 GO111MODULE 这个环境变量来作为 go module 的开关,这样代码就不用写在gopath目录下了,在命令行执行go env -w GO111MODULE=on
, go env -w GOPROXY=https://goproxy.cn,direct
设置代理用于go get安装第三方包,之后就可以开始写代码了,mkdir gostudy ,cd gostudy ,go mod init gostudy 初始化项目。
写代码少不了编辑器,推荐goland和vscode,我就用vs了,将gostudy 目录添加到工作区,ctrl+shift+p
输入 go install update tools
全选安装go相关的插件,还有vscode Code Runner
这个插件方便右键执行代码, rest client 插件方便http测试,先写个hello word:
vi test.go
package main
func main(){
println("hello word")
}
go run test.go
临时测试代码可以用在线网站https://go.dev/play , https://jsplay.goplus.org/ ,还方便生成分享链接,新建个测试文件 test.http 点send request即可请求。
//GET http://localhost:8090/welcome HTTP/1.1
GET http://httpbin.org/ip
### post
POST http://localhost:8090/companies HTTP/1.1
下载公众号话题
之前用python写了个公众号话题批量下载我又开发了个公众号音频视频和话题批量下载工具 ,以这个音频话题为例:https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MjM5NjAxOTU4MA==&action=getalbum&album_id=1777378132866465795&scene=173#wechat_redirect
这次用golang重写了下,代码如下:
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"
)
func Exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
func InArray(items []string, item string) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}
func Dwon() {
defer func() {
if err := recover(); err != nil {
fmt.Print("错误信息:")
fmt.Println(err)
}
}()
var url string
// https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MjM5NjAxOTU4MA==&action=getalbum&album_id=1777378132866465795&scene=173
fmt.Print("公众号苏生不惑提示你请输入话题地址:")
fmt.Scanln(&url)
if len(url) == 0 {
panic("话题地址为空")
}
client := &http.Client{}
reqest, err := http.NewRequest("GET", url, nil)
reqest.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 QBCore/4.0.1301.400 QQBrowser/9.0.2524.400 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2875.116 Safari/537.36 NetType/WIFI MicroMessenger/7.0.5 WindowsWechat")
if err != nil {
panic(err)
}
response, _ := client.Do(reqest)
defer response.Body.Close()
bResp, _ := io.ReadAll(response.Body)
// json, _ := simplejson.NewJson(bResp)
content := string(bResp)
var voiceids = regexp.MustCompile(`data-voiceid="(.*)"`).FindAllStringSubmatch(content, -1)
var titles = regexp.MustCompile(`data-title="(.*)" data-voiceid`).FindAllStringSubmatch(content, -1)
fileName := "wechat_topic_audio_list.txt"
// fmt.Print(voiceids, titles)
fileContent, _ := ioutil.ReadFile(fileName)
var voice_urls = regexp.MustCompile(`\n`).Split(string(fileContent), -1)
// fmt.Print(voice_urls)
var f2 *os.File
for k, v := range voiceids {
if InArray(voice_urls, "https://res.wx.qq.com/voice/getvoice?mediaid="+v[1]) {
fmt.Println("已经下载过音频:" + titles[k][1])
continue
}
res, _ := http.Get("https://res.wx.qq.com/voice/getvoice?mediaid=" + v[1])
f, _ := os.Create(titles[k][1] + ".mp3")
io.Copy(f, res.Body)
if Exists(fileName) {
f2, _ = os.OpenFile(fileName, os.O_APPEND, 0666)
} else {
f2, _ = os.Create(fileName)
}
defer f2.Close()
fmt.Println("正在下载音频:" + titles[k][1])
// _, _ = io.WriteString(f2, "https://res.wx.qq.com/voice/getvoice?mediaid="+v[1]+"\n")
// _, _ =f2.Write([]byte("https://res.wx.qq.com/voice/getvoice?mediaid="+v[1]+"\n"))
_, _ = f2.WriteString("https://res.wx.qq.com/voice/getvoice?mediaid=" + v[1] + "\n")
// _, _ = ioutil.WriteFile(fileName, []byte("https://res.wx.qq.com/voice/getvoice?mediaid="+v[1]+"\n"), 0666)
}
fmt.Print("下载完成")
}
下载效果:
定义的变量和导入的库必须使用,同一个package下的函数可以直接调用,不用大写开头,如果import 了新的库需要go get后go mod vendor才能运行,用go build main.go编译生成exe文件, 之前python打包的exe不支持win7,golang 打包的exe就可以支持win7了,另外windows还支持编译Linux执行文件 $env: goos="linux" $env: goarch=amd64
,整个项目也能生成一个gostudy.exe go build . && gostudy
常用包
gocloc 统计代码数 go install github.com/hhatto/gocloc/cmd/gocloc@latest
air自动启动程序
go install github.com/cosmtrek/air@latest 然后执行air,改完代码,Ctrl + s 后就会自动重新编译,不需要再执行命令 go run main.go,类似的还有gowatch https://github.com/silenceper/gowatch/blob/master/README_ZH_CN.md,gowatch -o ./bin/demo -p ./cmd/demo
carbon时间处理库 go get -u github.com/golang-module/carbon/v2
fmt.Sprintf("%s", carbon.Now()) // 2022-10-15 13:14:15
carbon.Now().ToString() // 2022-10-15 13:14:15 +0800 CST
carbon.Now().ToDateTimeString() // 2022-10-15 13:14:15
golang小抄 go install github.com/cheat/cheat/cmd/cheat@latest ,或者使用curl https://github.com/chubin/cheat.sh
lux 批量下载视频
go install github.com/iawia002/lux@latest
教程
Go Web 编程快速入门 测试生成图片调用https://www.bilibili.com/video/BV1Xv411k7Xn?p=28
python课程 https://www.bilibili.com/video/BV1kF411N7eq
golang入门到项目实战 2022最新Go语言教程,没有废话,纯干货!https://www.bilibili.com/video/BV1zR4y1t7Wj
go框架 https://www.bilibili.com/video/BV1SF411z7XW https://golang-tech-stack.com/tutorial/golang
Gin教程_Golang框架Gin入门实战教程-更新中https://www.bilibili.com/video/BV1fA411F7aM
golang 爬虫实战https://www.bilibili.com/video/BV1t5411Y7xQ
Golang爬虫技术教程https://www.bilibili.com/video/BV1H3411W7h4?p=2
基于 up 的教学构建了一个完整的 go web 小项目:https://github.com/fxtack/go-web-practice
【Go语言教程】五一小长假带你掌握一门编程(Golang)语言,从入门到精通 https://www.bilibili.com/video/BV1oZ4y1C74m
Golang2022 最新视频教程从入门到实战 https://www.bilibili.com/video/BV1Ar4y1a7oA https://github.com/CocaineCong/gin-mall
Go Web 编程快速入门【Golang/Go语言】(完结) https://www.bilibili.com/video/BV1Xv411k7Xn
视频+PPT下载: https://pan.baidu.com/s/18v_UdkI8UBkjUAW1xhxOjg?pwd=534y https://github.com/xpengkang/go-web-zero
基于gin框架和gorm的web开发实战 (七米出品) https://www.bilibili.com/video/BV1gJ411p7xC
2022最新版Go语言入门教程,十年大厂程序员讲解 https://www.bilibili.com/video/BV1SS4y1T7kJ
8小时转型GO语言 https://www.bilibili.com/video/BV1ma411E7uC
面向加薪 Go语言语法精练 2022最新Go语言教程,没有废话,纯干货!https://www.bilibili.com/video/BV1Ca411L72p
Go语言编程快速入门(Golang)(完结) https://www.bilibili.com/video/BV1fD4y1m7TD/
【幼麟实验室】Golang合辑 https://www.bilibili.com/video/BV1hv411x7we
8小时转职Golang工程师(如果你想低成本学习Go语言) https://www.bilibili.com/video/BV1gf4y1r79E https://www.yuque.com/aceld/mo95lb/pz6nag
笔记及资料 https://pan.baidu.com/s/1glckD7XGInHDFQQKCRE66g?pwd=gyj3
Golang学习记(更新中),Go语言零基础教程2022版 https://www.bilibili.com/video/BV1s341147US https://www.bilibili.com/read/readlist/rl496566
6小时成为Golang工程师 https://www.bilibili.com/video/BV1gQ4y1d7hN
写出Go优雅的程序代码 https://www.bilibili.com/video/BV1SK4y177Fs
Go语言-Golang Gin Go Web Gorm Go-Micro微服务教程 https://www.bilibili.com/video/BV1XY4y1t76G
10分钟轻松入门Go语言 | 知识点总结https://www.processon.com/view/link/5ecb3925f346fb6907154970#map
go面试题目 https://github.com/i-coder-robot/puzzle4go_season2 https://github.com/i-coder-robot/puzzle4go/tree/master/1-100
第一个Go程序 http://www.go-edu.cn/2022/04/21/go-03-%E7%AC%AC%E4%B8%80%E4%B8%AAGo%E7%A8%8B%E5%BA%8F/
Go语言入门学习路线 :https://blog.csdn.net/weixin_45304503/article/details/127041166
GitHub地址:https://github.com/CocaineCong/Golang-Learning https://www.bilibili.com/video/BV1WG4y1x7uU
Go learning materials,涵盖基础、中级和高级教程 https://github.com/jincheng9/go-tutorial
入门文档 https://gobyexample-cn.github.io/string-formatting ,https://tour.go-zh.org/list ,https://gfw.go101.org/article/101.html
命令行看文档 go doc regexp/syntax
Go 语言趣学指南 https://github.com/nathany/get-programming-with-go
语法实例 https://github.com/i-coder-robot/go_basic_edu
golang 小抄 https://quickref.me/golang
X分钟速成go https://learnxinyminutes.com/docs/zh-cn/go-cn/
正则表达式入门教程 https://zq99299.github.io/note-book/regular/
在线写代码 https://riju.codes/ 这个网站提供了200多种语言的在线运行环境
JSON 转结构体 https://mholt.github.io/json-to-go
go专题 https://www.liwenzhou.com/posts/Go/golang-menu/
Go语言精进之路 https://golang.coding3min.com/ https://geektutu.com/post/quick-go-gin.html