📅  最后修改于: 2023-12-03 15:01:02.339000             🧑  作者: Mango
在进行 Web 开发中,经常需要检查网站的状态以确保服务的正常运行。这时候用 Golang 就可以轻松实现一个程序来检查 URL 网站的状态。本文将介绍如何使用 Golang 实现这一功能。
Golang 的 net/http 包提供了方便的 HTTP 客户端和服务器实现。我们可以使用该包中提供的 Get 方法来发送 HTTP GET 请求,然后根据返回的 Response 来检查 URL 网站的状态。
代码如下:
package main
import (
"fmt"
"net/http"
)
func main() {
url := "https://www.google.com"
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
status := resp.StatusCode
if status == 200 {
fmt.Println("Site", url, "is UP!")
} else {
fmt.Println("Site", url, "is DOWN!")
}
}
markdown 格式代码片段:
```go
package main
import (
"fmt"
"net/http"
)
func main() {
url := "https://www.google.com"
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
status := resp.StatusCode
if status == 200 {
fmt.Println("Site", url, "is UP!")
} else {
fmt.Println("Site", url, "is DOWN!")
}
}
Output:
Site https://www.google.com is UP!
## 二、使用 goroutine 并发检查多个网站状态
当需要检查多个 URL 网站状态时,我们可以使用 goroutine 来实现并发检查。使用 goroutine 可以充分利用 CPU,提高检查速度。
```go
package main
import (
"fmt"
"net/http"
"sync"
)
func main() {
urls := []string{
"https://www.google.com",
"https://www.baidu.com",
"https://www.sina.com",
}
var wg sync.WaitGroup
for _, url := range urls {
wg.Add(1)
go func(url string) {
defer wg.Done()
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error:", err)
} else {
defer resp.Body.Close()
status := resp.StatusCode
if status == 200 {
fmt.Println("Site", url, "is UP!")
} else {
fmt.Println("Site", url, "is DOWN!")
}
}
}(url)
}
wg.Wait()
}
markdown 格式代码片段:
```go
package main
import (
"fmt"
"net/http"
"sync"
)
func main() {
urls := []string{
"https://www.google.com",
"https://www.baidu.com",
"https://www.sina.com",
}
var wg sync.WaitGroup
for _, url := range urls {
wg.Add(1)
go func(url string) {
defer wg.Done()
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error:", err)
} else {
defer resp.Body.Close()
status := resp.StatusCode
if status == 200 {
fmt.Println("Site", url, "is UP!")
} else {
fmt.Println("Site", url, "is DOWN!")
}
}
}(url)
}
wg.Wait()
}
Output:
Site https://www.baidu.com is UP!
Site https://www.sina.com is UP!
Site https://www.google.com is UP!
当需要输出检查结果到文件时,我们可以使用 os 包中的 File 类型来实现。在每个 goroutine 中打开文件,将检查结果写入文件中。
代码如下:
package main
import (
"fmt"
"net/http"
"os"
"sync"
)
func main() {
urls := []string{
"https://www.google.com",
"https://www.baidu.com",
"https://www.sina.com",
}
file, err := os.OpenFile("result.txt", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
fmt.Println("Error:", err)
return
}
defer file.Close()
var wg sync.WaitGroup
for _, url := range urls {
wg.Add(1)
go func(url string) {
defer wg.Done()
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error:", err)
} else {
defer resp.Body.Close()
status := resp.StatusCode
result := fmt.Sprintf("Site %s is ", url)
if status == 200 {
result += "UP!\n"
} else {
result += "DOWN!\n"
}
file.WriteString(result)
}
}(url)
}
wg.Wait()
}
markdown 格式代码片段:
```go
package main
import (
"fmt"
"net/http"
"os"
"sync"
)
func main() {
urls := []string{
"https://www.google.com",
"https://www.baidu.com",
"https://www.sina.com",
}
file, err := os.OpenFile("result.txt", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
fmt.Println("Error:", err)
return
}
defer file.Close()
var wg sync.WaitGroup
for _, url := range urls {
wg.Add(1)
go func(url string) {
defer wg.Done()
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error:", err)
} else {
defer resp.Body.Close()
status := resp.StatusCode
result := fmt.Sprintf("Site %s is ", url)
if status == 200 {
result += "UP!\n"
} else {
result += "DOWN!\n"
}
file.WriteString(result)
}
}(url)
}
wg.Wait()
}
Output:
执行后,将在工作目录下生成 result.txt 文件,其内容为:
Site https://www.google.com is UP!
Site https://www.baidu.com is UP!
Site https://www.sina.com is UP!