fmt.Fprintf
时间: 2024-01-23 07:15:22 浏览: 70
`fmt.Fprintf`是Go语言中的一个函数,用于将格式化的数据写入到一个输出流中。它的语法如下:
```go
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)
```
其中,`w`是一个实现了`io.Writer`接口的对象,可以是文件、网络连接等;`format`是一个字符串,用于指定输出的格式;`a`是一个可变参数,用于传递要格式化的数据。
下面是一个使用`fmt.Fprintf`的例子:
```go
package main
import (
"fmt"
"os"
)
func main() {
name := "Alice"
age := 25
fmt.Fprintf(os.Stdout, "Name: %s, Age: %d\n", name, age)
}
```
上述代码将会在标准输出中打印出`Name: Alice, Age: 25`。
相关问题
package main import ( "fmt" "html/template" "log" "net/http" "strings" ) func login(w http.ResponseWriter, r *http.Request) { fmt.Println("method:", r.Method) if r.Method == "GET" { t, err := template.ParseFiles("H:\\Goproject\\src\\web\\login.html") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := t.Execute(w, nil); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } else { _ = r.ParseForm() fmt.Println("username:", r.Form["username"]) fmt.Println("password:", r.Form["password"]) if pwd := r.Form.Get("password"); pwd == "123456" { fmt.Fprintf(w, "欢迎登陆,Hello %s!", r.Form.Get("username")) } else { fmt.Fprintf(w, "密码错误!") } } } func sayHello(w http.ResponseWriter, r *http.Request) { _ = r.ParseForm() //3 解析参数,默认不解析 fmt.Println(r.Form) //4 输出到服务器端的打印信息 fmt.Println("Path:", r.URL.Path) for k, v := range r.Form { fmt.Println("key:", k) fmt.Println("value", strings.Join(v, "")) } fmt.Fprintf(w, "Hello aoho") //5 写入到w的是输出到客户端的内容 } func main() { http.HandleFunc("/", login) http.HandleFunc("/sayHello", sayHello) //1 设置访问的路由 err := http.ListenAndServe(":8080", nil) //2 设置监听的端口 if err != nil { log.Fatal("ListenSever:", err) } } 修改为绝对路径可以运行,但是如何修改为相对路径
要将路径修改为相对路径,您需要使用`http.Dir`函数将文件夹路径转换为`http.FileServer`类型。这样可以指定要提供给用户的静态文件所在的文件夹。以下是对代码的修改:
```go
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"strings"
)
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method)
if r.Method == "GET" {
t, err := template.ParseFiles("login.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := t.Execute(w, nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
} else {
_ = r.ParseForm()
fmt.Println("username:", r.Form["username"])
fmt.Println("password:", r.Form["password"])
if pwd := r.Form.Get("password"); pwd == "123456" {
fmt.Fprintf(w, "欢迎登陆,Hello %s!", r.Form.Get("username"))
} else {
fmt.Fprintf(w, "密码错误!")
}
}
}
func sayHello(w http.ResponseWriter, r *http.Request) {
_ = r.ParseForm() //3 解析参数,默认不解析
fmt.Println(r.Form) //4 输出到服务器端的打印信息
fmt.Println("Path:", r.URL.Path)
for k, v := range r.Form {
fmt.Println("key:", k)
fmt.Println("value", strings.Join(v, ""))
}
fmt.Fprintf(w, "Hello aoho") //5 写入到w的是输出到客户端的内容
}
func main() {
http.HandleFunc("/", login)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/sayHello", sayHello) //1 设置访问的路由
err := http.ListenAndServe(":8080", nil) //2 设置监听的端口
if err != nil {
log.Fatal("ListenSever:", err)
}
}
```
这里假设您的静态资源文件夹名为`static`,并且在同一级目录下。使用`http.Handle`和`http.StripPrefix`函数来处理静态文件路由,并指定静态文件夹的相对路径。修改后,您应该能够使用相对路径来访问静态文件。
GO sql= fmt.Fprintf("AND label LIKE %s", startT )
这段代码看起来是 Golang 语言中的代码,和 Python 语言中的字符串格式化操作略有不同。在 Golang 中,也可以使用 fmt.Printf()、fmt.Sprintf() 等函数来进行字符串格式化操作,但是占位符和格式化指令都略有不同。
在这段代码中,%s 仍然是一个占位符,表示将要替换成一个字符串类型的值。startT 则是一个变量名,表示要替换成的实际值。因此,这段代码的作用是将字符串 "AND label LIKE %s" 中的 %s 替换成 startT 的值,并将结果赋值给变量 sql。具体的实现方式是使用 fmt.Fprintf() 函数来进行字符串格式化操作,代码如下:
```go
var buf bytes.Buffer
fmt.Fprintf(&buf, "AND label LIKE %s", startT)
sql := buf.String()
```
在这里,我们使用了一个 bytes.Buffer 对象来缓存格式化后的字符串。然后,我们调用 fmt.Fprintf() 函数来将 "AND label LIKE %s" 和 startT 进行字符串格式化操作,并将结果输出到 buf 中。最后,我们调用 buf.String() 方法来获取 buf 中的字符串内容,并将其赋值给 sql 变量。这样,我们就得到了一个安全的 SQL 查询语句,避免了 SQL 注入攻击的风险。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)