此页内容

Golang

pengzhanbo

387字约1分钟

2024-04-22

概述

主题提供了 Golang 代码演示,支持 在线运行 Go 代码。

重要

该功能通过将 代码提交到 服务器 进行 编译并执行,且一次只能提交单个代码文件。

因此,请不要使用此功能 执行 过于复杂的代码,也不要过于频繁的进行执行请求。

配置

该功能默认不启用,你可以通过配置来启用它。

.vuepress/config.ts
export default defineUserConfig({
  theme: plumeTheme({
    plugins: {
      markdownPower: {
        repl: true,
      },
    }
  })
})

使用

使用 ::: go-repl 容器语法 将 Go 代码块包裹起来。主题会检查代码块并添加执行按钮。

::: go-repl
```go
// your rust code
```
:::

示例

打印内容

输入:

:::go-repl
```go
package main

import (
  "fmt"
)

func main() {
  fmt.Println("Hello World")
}
```
:::

输出:

package main

import (
  "fmt"
)

func main() {
  fmt.Println("Hello World")
}

循环随机延迟打印

输入:

:::go-repl
```go
package main

import (
  "fmt"
  "math/rand"
  "time"
)

func main() {
  for i := 0; i < 10; i++ {
    dur := time.Duration(rand.Intn(1000)) * time.Millisecond
    fmt.Printf("Sleeping for %v\n", dur)
    // Sleep for a random duration between 0-1000ms
    time.Sleep(dur)
  }
  fmt.Println("Done!")
}
```
:::

输出:

package main

import (
  "fmt"
  "math/rand"
  "time"
)

func main() {
  for i := 0; i < 10; i++ {
    dur := time.Duration(rand.Intn(1000)) * time.Millisecond
    fmt.Printf("Sleeping for %v\n", dur)
    // Sleep for a random duration between 0-1000ms
    time.Sleep(dur)
  }
  fmt.Println("Done!")
}

网络请求

package main

import (
  "fmt"
  "io"
  "log"
  "net"
  "net/http"
  "os"
)

func main() {
  http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello, playground")
  })

  log.Println("Starting server...")
  l, err := net.Listen("tcp", "localhost:8080")
  if err != nil {
    log.Fatal(err)
  }
  go func() {
    log.Fatal(http.Serve(l, nil))
  }()

  log.Println("Sending request...")
  res, err := http.Get("http://localhost:8080/hello")
  if err != nil {
    log.Fatal(err)
  }

  log.Println("Reading response...")
  if _, err := io.Copy(os.Stdout, res.Body); err != nil {
    log.Fatal(err)
  }
}