Standard library / net/textproto
textproto.Conn.Cmd()
Allocate a sequence ID, wait for its request turn and write a formatted command. Use the ID to pair StartResponse and EndResponse; do not insert untrusted newlines. Expected stdout: '0 "HELP\\r\\n"\n250\n'.
Formal Syntax & Structure
go syntaxgo
func (c *Conn) Cmd(format string, args ...any) (id uint, err error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| format | string | fmt-style format string; do not allow untrusted line breaks in commands. |
| args | ...any | Optional values for format placeholders. |
Return Value
(id uint, err error)
Allocated request ID and write error.
Working Example
Example Snippetgo
package main
import ( "bufio" "fmt" "io" "net" "net/textproto" "time")
func main() { c, s := net.Pipe() defer c.Close() defer s.Close() check(c.SetDeadline(time.Now().Add(3 * time.Second))) check(s.SetDeadline(time.Now().Add(3 * time.Second))) tp := textproto.NewConn(c) defer tp.Close() done := make(chan string, 1) go func() { line := must(bufio.NewReader(s).ReadString(10)) done <- line must(io.WriteString(s, "250 ready\r\n")) }() id := must(tp.Cmd("HELP")) fmt.Printf("%d %q\n", id, <-done) tp.StartResponse(id) code, _, e := tp.ReadCodeLine(250) check(e) tp.EndResponse(id) fmt.Println(code)}
func must[T any](v T, e error) T { if e != nil { panic(e) } return v}func check(e error) { if e != nil { panic(e) }}