Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: text/template: Template.Scan #66707

Closed
3052 opened this issue Apr 6, 2024 · 1 comment
Closed

proposal: text/template: Template.Scan #66707

3052 opened this issue Apr 6, 2024 · 1 comment
Labels
Milestone

Comments

@3052
Copy link

3052 commented Apr 6, 2024

Proposal Details

currently you can implement fmt.Fprintf like this:

package main

import (
   "io"
   "os"
   "text/template"
)

func fprintf(w io.Writer, format string, a any) error {
   text, err := new(template.Template).Parse(format)
   if err != nil {
      return err
   }
   return text.Execute(w, a)
}

func main() {
   var a struct {
      Hello string
      World string
   }
   a.Hello = "hello"
   a.World = "world"
   fprintf(os.Stdout, "http://example.com/{{.Hello}}/{{.World}}", a)
}

but you cannot do the opposite. ideally some method like this would exist:

func (t *Template) Scan(rd io.Reader, data any) error

then you could implement fmt.Fscanf:

package main

import (
   "io"
   "os"
   "text/template"
)

func fscanf(r io.Reader, format string, a any) error {
   text, err := new(template.Template).Parse(format)
   if err != nil {
      return err
   }
   return text.Scan(r, a)
}

func main() {
   r := strings.NewReader("http://example.com/hello/world")
   var a struct {
      Hello string
      World string
   }
   fscanf(r, "http://example.com/{{.Hello}}/{{.World}}", &a)
}
@3052 3052 added the Proposal label Apr 6, 2024
@gopherbot gopherbot added this to the Proposal milestone Apr 6, 2024
@rittneje
Copy link

rittneje commented Apr 6, 2024

I think this is a duplicate of #52473.

@3052 3052 closed this as completed Apr 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants