go: play with so source code. Go源代码编译和调试

工具

vscode

clone仓库

git clone https://github.com/golang/go
cd golang/go

编译go

编译的主要入口是src/make.bash(windows则是src/make.bat):

# src/make.bash

# GOROOT_FINAL: The expected final Go root, baked into binaries.
# The default is the location of the Go tree during the build.
#
# GOROOT_BOOTSTRAP: A working Go tree >= Go 1.4 for bootstrap.
# If $GOROOT_BOOTSTRAP/bin/go is missing, $(go env GOROOT) is
# tried for all "go" in $PATH. $HOME/go1.4 by default.
if [ ! -f run.bash ]; then
	echo 'make.bash must be run from $GOROOT/src' 1>&2
	exit 1
fi
...

有两个环境变量值得注意:

  • GOROOT_FINAL 最终输出的编译的go的目录
  • GOROOT_BOOTSTRAP 用于编译go的基础go版本,必须>=go1.4

必须在src目录运行make.bash

cd src
rm -rf build_go && mkdir -p build_go
GOROOT_FINAL=build_go ./make.bash

日志:

Building Go cmd/dist using /Users/xhd2015/installed/go1.17. (go1.17.5 darwin/amd64)
Building Go toolchain1 using /Users/xhd2015/installed/go1.17.
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 darwin/amd64.
---
Installed Go for darwin/amd64 in /Users/xhd2015/Projects/gopath/src/github.com/golang/go
Installed commands in /Users/xhd2015/Projects/gopath/src/github.com/golang/go/bin

The binaries expect /Users/xhd2015/Projects/gopath/src/github.com/golang/go to be copied or moved to build_go

运行编译完成的go;

$ GOROOT=$PWD bin/go version
go version devel go1.19-416c953960 Sun Jun 26 00:26:59 2022 +0000 darwin/amd64

误区1: 直接调试 src/cmd/go

虽然src/cmd/go能够编译成go,但是如果直接使用系统的工具编译:

cd src/cmd
go build -o cmd_go ./go
./cmd_go # 这里实际是以系统的源码进行编译的

必须使用src/make.bash才能编译出当前目录的go

调试src/cmd/go

GOROOT=../.. ../../bin/go build -gcflags=all="-N -l" -o /tmp/go/cmd/cmd_go.bin ./go

在这里插入图片描述

IDE

vscode需要设置go.goroot:

  "go.goroot": "/Users/xhd2015/Projects/gopath/src/github.com/golang/go",

并重启。这样才能解析到对应的符号。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是 Python 代码,可以实现您的需求。请确保您已经安装了 requests, BeautifulSoup, gTTS 和 pygame 等库。 ```python import requests from bs4 import BeautifulSoup from gtts import gTTS import pygame # 获取网页内容并用 BeautifulSoup 解析 url = 'https://so.gushiwen.cn/gushi/tangshi.aspx' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} response = requests.get(url, headers=headers) soup = BeautifulSoup(response.content, 'html.parser') # 获取古诗列表 poems = soup.find_all('div', class_='typecont') # 选择一首古诗并获取标题、作者、内容 chosen_poem = poems[0] title = chosen_poem.find('b').text author = chosen_poem.find_all('p')[1].text content = chosen_poem.find('div', class_='contson').text.strip() # 保存为 txt 文件 filename = f'{title}.txt' with open(filename, 'w', encoding='utf-8') as f: f.write(f'{title}\n\n{author}\n\n{content}') # 保存为 mp3 文件 tts = gTTS(content, lang='zh-cn') audio_filename = f'{title}.mp3' tts.save(audio_filename) # 播放 mp3 文件 pygame.mixer.init() pygame.mixer.music.load(audio_filename) pygame.mixer.music.play() ``` 请注意,由于该网站的内容为繁体字,所以生成的语音也是繁体字读音。如果需要将其转换为简体字,可以通过其他库或工具进行转换。另外,由于该网站的内容较多,所以代码中只选择了第一首古诗作为示例。若需要选择其他古诗,请自行更改代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值