day7函数 练习
parent
5ab496ae36
commit
2734edb601
|
@ -67,7 +67,7 @@ func square() func() int {
|
|||
// 而是通过resp.Request.URL解析后的值。
|
||||
// 解析后,这些连接以绝对路径的形式存在,可以直接被http.Get访问。
|
||||
func Extract() ([]string, error) {
|
||||
node, resp := HtmlTools.GetHtmlS()
|
||||
node, resp := HtmlTools.GetHtmlResponse("")
|
||||
var links []string
|
||||
visitNode := func(n *html.Node) {
|
||||
if n.Type == html.ElementNode && n.Data == "a" {
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package HtmlTools
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// see https://zhuanlan.zhihu.com/p/363714760
|
||||
// 最终方案-全兼容
|
||||
func GetCurrentAbPath() string {
|
||||
dir := getCurrentAbPathByExecutable()
|
||||
if strings.Contains(dir, getTmpDir()) {
|
||||
return getCurrentAbPathByCaller()
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
// 获取系统临时目录,兼容go run
|
||||
func getTmpDir() string {
|
||||
dir := os.Getenv("TEMP")
|
||||
if dir == "" {
|
||||
dir = os.Getenv("TMP")
|
||||
}
|
||||
res, _ := filepath.EvalSymlinks(dir)
|
||||
return res
|
||||
}
|
||||
|
||||
// 获取当前执行文件绝对路径
|
||||
func getCurrentAbPathByExecutable() string {
|
||||
exePath, err := os.Executable()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
res, _ := filepath.EvalSymlinks(filepath.Dir(exePath))
|
||||
return res
|
||||
}
|
||||
|
||||
// 获取当前执行文件绝对路径(go run)
|
||||
func getCurrentAbPathByCaller() string {
|
||||
var abPath string
|
||||
_, filename, _, ok := runtime.Caller(0)
|
||||
if ok {
|
||||
abPath = path.Dir(filename)
|
||||
}
|
||||
return abPath
|
||||
}
|
Loading…
Reference in New Issue