sntshk
(Santosh Kumar)
February 7, 2020, 1:24pm
1
Initially, my code was like:
t := template.Must(template.New("").Parse(someInfileTemplate))
err = t.Execute(os.Stdout, links)
I was able to render and print the template to the os.Stdout.
Now I have expanded the code and am taking a outFileFlag
which is a String flag from argparse module which returns a pointer to string. argparse should not be a problem because stdlib flag
module returns the same pointer to string.
I tried using ioutil.WriteFile but got confused.
How do I convert a pointer to a string to io.Writer
which template.Execute
takes?
lutzhorn
(Lutz Horn)
February 7, 2020, 1:47pm
2
sntshk:
t.Execute
What is the signature of this method? What is the type of the first parameter: os.File
?
NobbZ
(Norbert Melzer)
February 7, 2020, 1:50pm
3
Its probably text.template.(*Template).Execute()
or its html
equivalent.
sntshk
(Santosh Kumar)
February 7, 2020, 1:51pm
4
I have updated the question.
t
is template.Must(template.New("").Parse(someInfileTemplate))
NobbZ
(Norbert Melzer)
February 7, 2020, 1:55pm
5
You can not make it take a filename directly.
You need to use os.OpenFile()
to get an *os.File
, which implements io.Writer
.
1 Like
sntshk
(Santosh Kumar)
February 7, 2020, 1:59pm
6
f, err := os.Open(*outFileFlag)
if err != nil {
exit("Error opening the out file.")
}
t := template.Must(template.New("").Parse(someInfileTemplate))
err = t.Execute(f, links)
if err != nil {
exit("Cannot execute the template")
}
The program exits at first exit().
NobbZ
(Norbert Melzer)
February 7, 2020, 2:01pm
7
os.Open
opens for reading, as I said you need os.OpenFile()
.
And perhaps you should actually fmt.Print()
the err
. Thats how you learn what went wrong.
sntshk
(Santosh Kumar)
February 7, 2020, 2:07pm
8
f, err := os.OpenFile(*outFileFlag, os.O_CREATE, 644)
if err != nil {
panic(err)
}
This would work on any other file, but won’t on os.Stdout:
panic: open os.Stdout: permission denied
NobbZ
(Norbert Melzer)
February 7, 2020, 2:10pm
9
I do not understand. How do you pass os.Stdout
there?
No need to open Stdout. It is already ready to use as it is defined in os.go
var (
Stdin = NewFile(uintptr(syscall.Stdin), “/dev/stdin”)
Stdout = NewFile(uintptr(syscall.Stdout), “/dev/stdout”)
Stderr = NewFile(uintptr(syscall.Stderr), “/dev/stderr”)
)
sntshk
(Santosh Kumar)
February 10, 2020, 5:47am
11
You are right! I have decided to use some predefined filename now.
NobbZ
(Norbert Melzer)
February 10, 2020, 5:58am
12
Why predefined? You can still use flags
to get it from the user as a command line argument. But using os.Stdout
doesn’t make sense in that scenario.
Especially not as a filename…
That is why I’m confused.
sntshk
(Santosh Kumar)
February 10, 2020, 6:04am
13
Yes, that is what I mean. Users can still override the flag.
The thing which is predefined is default file name.
NobbZ
(Norbert Melzer)
February 10, 2020, 6:09am
14
Okay, I do understand now.
Have you considered following some linux convention and use -
as the default filename, meaning os.Stdout
?
func writerFromFlag(flag *string) (io.Writer, error) {
if *flag == "-" {
return os.Stdout, err
}
return os.OpenFile(*flag, os.O_CREATE, 644)
}
system
(system)
Closed
May 10, 2020, 6:09am
15
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.