Golang template for source and target printing
I want to generate go template with loop which looks like following
target1:
echo from target2
target2:
echo from target2
targetN:
echo from targetN
This simply creates a target and line after
it prints from which target it's called.
I've tried with the following which works partially, any Idea what I miss here ? or how can I simplify the code in t.Execute
https://play.golang.org/p/iLWQANobKUL
package main
import (
"bytes"
"fmt"
"html/template"
)
func main() {
t := template.Must(template.New("").Parse(` {{- range .}} {{.Name}}:{{end}}
{{- range .}}
{{.Name}}:
{{"t"}}echo from {{.Name}} {{.Text}}
{{end}}
`))
var buf bytes.Buffer
t.Execute(&buf, interface{}{map[string]string{"Name": "target1", "Text": "echo"}})
fmt.Printf("%qn", buf.String())
}
templates go
add a comment |
I want to generate go template with loop which looks like following
target1:
echo from target2
target2:
echo from target2
targetN:
echo from targetN
This simply creates a target and line after
it prints from which target it's called.
I've tried with the following which works partially, any Idea what I miss here ? or how can I simplify the code in t.Execute
https://play.golang.org/p/iLWQANobKUL
package main
import (
"bytes"
"fmt"
"html/template"
)
func main() {
t := template.Must(template.New("").Parse(` {{- range .}} {{.Name}}:{{end}}
{{- range .}}
{{.Name}}:
{{"t"}}echo from {{.Name}} {{.Text}}
{{end}}
`))
var buf bytes.Buffer
t.Execute(&buf, interface{}{map[string]string{"Name": "target1", "Text": "echo"}})
fmt.Printf("%qn", buf.String())
}
templates go
add a comment |
I want to generate go template with loop which looks like following
target1:
echo from target2
target2:
echo from target2
targetN:
echo from targetN
This simply creates a target and line after
it prints from which target it's called.
I've tried with the following which works partially, any Idea what I miss here ? or how can I simplify the code in t.Execute
https://play.golang.org/p/iLWQANobKUL
package main
import (
"bytes"
"fmt"
"html/template"
)
func main() {
t := template.Must(template.New("").Parse(` {{- range .}} {{.Name}}:{{end}}
{{- range .}}
{{.Name}}:
{{"t"}}echo from {{.Name}} {{.Text}}
{{end}}
`))
var buf bytes.Buffer
t.Execute(&buf, interface{}{map[string]string{"Name": "target1", "Text": "echo"}})
fmt.Printf("%qn", buf.String())
}
templates go
I want to generate go template with loop which looks like following
target1:
echo from target2
target2:
echo from target2
targetN:
echo from targetN
This simply creates a target and line after
it prints from which target it's called.
I've tried with the following which works partially, any Idea what I miss here ? or how can I simplify the code in t.Execute
https://play.golang.org/p/iLWQANobKUL
package main
import (
"bytes"
"fmt"
"html/template"
)
func main() {
t := template.Must(template.New("").Parse(` {{- range .}} {{.Name}}:{{end}}
{{- range .}}
{{.Name}}:
{{"t"}}echo from {{.Name}} {{.Text}}
{{end}}
`))
var buf bytes.Buffer
t.Execute(&buf, interface{}{map[string]string{"Name": "target1", "Text": "echo"}})
fmt.Printf("%qn", buf.String())
}
templates go
templates go
edited Nov 24 '18 at 10:11
asked Nov 24 '18 at 10:05
user6124024
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Is this what you need?
package main
import (
"text/template"
"os"
)
func main() {
t := template.Must(template.New("").Parse(`{{- range .}}{{.}}:
echo from {{.}}
{{end}}
`))
t.Execute(os.Stdout, string{"target1", "target2", "target3"})
}
Output:
target1:
echo from target1
target2:
echo from target2
target3:
echo from target3
Ref: https://golang.org/pkg/text/template/
Update:
If you want to use the pipeline as a map and want to use value from the value of the corresponding key, then:
package main
import (
"text/template"
"os"
)
func main() {
t := template.Must(template.New("").Parse(`{{- range $key, $val := .}}{{$key}}:
echo from {{$val}}
{{end -}}
`))
t.Execute(os.Stdout, map[string]string{"target1": "foo1", "target2": "bar2", "target3": "baz"})
}
Output:
target1:
echo from foo1
target2:
echo from bar2
target3:
echo from baz
yes 1+, thanks a lot. btw if I want to use some key value i mean everthing is the same but just the value of the echo will be the value, i.e. {target1:foo1 ,target2:bar2,target3:baz}` and the complite value will be the same just the echo for targets liketarget2
echo will beecho from bar2
and fortarget3
will beecho from baz
– user6124024
Nov 24 '18 at 19:14
I have edited my answer .
– Shudipta Sharma
Nov 24 '18 at 19:42
Amazing thank you very much!
– user6124024
Nov 24 '18 at 19:43
Welcome! Best of luck.
– Shudipta Sharma
Nov 24 '18 at 19:45
Thank you again :) , I've another question which is related, please see if you can help , stackoverflow.com/questions/53461812/…
– user6124024
Nov 24 '18 at 19:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457076%2fgolang-template-for-source-and-target-printing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this what you need?
package main
import (
"text/template"
"os"
)
func main() {
t := template.Must(template.New("").Parse(`{{- range .}}{{.}}:
echo from {{.}}
{{end}}
`))
t.Execute(os.Stdout, string{"target1", "target2", "target3"})
}
Output:
target1:
echo from target1
target2:
echo from target2
target3:
echo from target3
Ref: https://golang.org/pkg/text/template/
Update:
If you want to use the pipeline as a map and want to use value from the value of the corresponding key, then:
package main
import (
"text/template"
"os"
)
func main() {
t := template.Must(template.New("").Parse(`{{- range $key, $val := .}}{{$key}}:
echo from {{$val}}
{{end -}}
`))
t.Execute(os.Stdout, map[string]string{"target1": "foo1", "target2": "bar2", "target3": "baz"})
}
Output:
target1:
echo from foo1
target2:
echo from bar2
target3:
echo from baz
yes 1+, thanks a lot. btw if I want to use some key value i mean everthing is the same but just the value of the echo will be the value, i.e. {target1:foo1 ,target2:bar2,target3:baz}` and the complite value will be the same just the echo for targets liketarget2
echo will beecho from bar2
and fortarget3
will beecho from baz
– user6124024
Nov 24 '18 at 19:14
I have edited my answer .
– Shudipta Sharma
Nov 24 '18 at 19:42
Amazing thank you very much!
– user6124024
Nov 24 '18 at 19:43
Welcome! Best of luck.
– Shudipta Sharma
Nov 24 '18 at 19:45
Thank you again :) , I've another question which is related, please see if you can help , stackoverflow.com/questions/53461812/…
– user6124024
Nov 24 '18 at 19:51
add a comment |
Is this what you need?
package main
import (
"text/template"
"os"
)
func main() {
t := template.Must(template.New("").Parse(`{{- range .}}{{.}}:
echo from {{.}}
{{end}}
`))
t.Execute(os.Stdout, string{"target1", "target2", "target3"})
}
Output:
target1:
echo from target1
target2:
echo from target2
target3:
echo from target3
Ref: https://golang.org/pkg/text/template/
Update:
If you want to use the pipeline as a map and want to use value from the value of the corresponding key, then:
package main
import (
"text/template"
"os"
)
func main() {
t := template.Must(template.New("").Parse(`{{- range $key, $val := .}}{{$key}}:
echo from {{$val}}
{{end -}}
`))
t.Execute(os.Stdout, map[string]string{"target1": "foo1", "target2": "bar2", "target3": "baz"})
}
Output:
target1:
echo from foo1
target2:
echo from bar2
target3:
echo from baz
yes 1+, thanks a lot. btw if I want to use some key value i mean everthing is the same but just the value of the echo will be the value, i.e. {target1:foo1 ,target2:bar2,target3:baz}` and the complite value will be the same just the echo for targets liketarget2
echo will beecho from bar2
and fortarget3
will beecho from baz
– user6124024
Nov 24 '18 at 19:14
I have edited my answer .
– Shudipta Sharma
Nov 24 '18 at 19:42
Amazing thank you very much!
– user6124024
Nov 24 '18 at 19:43
Welcome! Best of luck.
– Shudipta Sharma
Nov 24 '18 at 19:45
Thank you again :) , I've another question which is related, please see if you can help , stackoverflow.com/questions/53461812/…
– user6124024
Nov 24 '18 at 19:51
add a comment |
Is this what you need?
package main
import (
"text/template"
"os"
)
func main() {
t := template.Must(template.New("").Parse(`{{- range .}}{{.}}:
echo from {{.}}
{{end}}
`))
t.Execute(os.Stdout, string{"target1", "target2", "target3"})
}
Output:
target1:
echo from target1
target2:
echo from target2
target3:
echo from target3
Ref: https://golang.org/pkg/text/template/
Update:
If you want to use the pipeline as a map and want to use value from the value of the corresponding key, then:
package main
import (
"text/template"
"os"
)
func main() {
t := template.Must(template.New("").Parse(`{{- range $key, $val := .}}{{$key}}:
echo from {{$val}}
{{end -}}
`))
t.Execute(os.Stdout, map[string]string{"target1": "foo1", "target2": "bar2", "target3": "baz"})
}
Output:
target1:
echo from foo1
target2:
echo from bar2
target3:
echo from baz
Is this what you need?
package main
import (
"text/template"
"os"
)
func main() {
t := template.Must(template.New("").Parse(`{{- range .}}{{.}}:
echo from {{.}}
{{end}}
`))
t.Execute(os.Stdout, string{"target1", "target2", "target3"})
}
Output:
target1:
echo from target1
target2:
echo from target2
target3:
echo from target3
Ref: https://golang.org/pkg/text/template/
Update:
If you want to use the pipeline as a map and want to use value from the value of the corresponding key, then:
package main
import (
"text/template"
"os"
)
func main() {
t := template.Must(template.New("").Parse(`{{- range $key, $val := .}}{{$key}}:
echo from {{$val}}
{{end -}}
`))
t.Execute(os.Stdout, map[string]string{"target1": "foo1", "target2": "bar2", "target3": "baz"})
}
Output:
target1:
echo from foo1
target2:
echo from bar2
target3:
echo from baz
edited Nov 24 '18 at 19:41
answered Nov 24 '18 at 12:19
Shudipta SharmaShudipta Sharma
1,185413
1,185413
yes 1+, thanks a lot. btw if I want to use some key value i mean everthing is the same but just the value of the echo will be the value, i.e. {target1:foo1 ,target2:bar2,target3:baz}` and the complite value will be the same just the echo for targets liketarget2
echo will beecho from bar2
and fortarget3
will beecho from baz
– user6124024
Nov 24 '18 at 19:14
I have edited my answer .
– Shudipta Sharma
Nov 24 '18 at 19:42
Amazing thank you very much!
– user6124024
Nov 24 '18 at 19:43
Welcome! Best of luck.
– Shudipta Sharma
Nov 24 '18 at 19:45
Thank you again :) , I've another question which is related, please see if you can help , stackoverflow.com/questions/53461812/…
– user6124024
Nov 24 '18 at 19:51
add a comment |
yes 1+, thanks a lot. btw if I want to use some key value i mean everthing is the same but just the value of the echo will be the value, i.e. {target1:foo1 ,target2:bar2,target3:baz}` and the complite value will be the same just the echo for targets liketarget2
echo will beecho from bar2
and fortarget3
will beecho from baz
– user6124024
Nov 24 '18 at 19:14
I have edited my answer .
– Shudipta Sharma
Nov 24 '18 at 19:42
Amazing thank you very much!
– user6124024
Nov 24 '18 at 19:43
Welcome! Best of luck.
– Shudipta Sharma
Nov 24 '18 at 19:45
Thank you again :) , I've another question which is related, please see if you can help , stackoverflow.com/questions/53461812/…
– user6124024
Nov 24 '18 at 19:51
yes 1+, thanks a lot. btw if I want to use some key value i mean everthing is the same but just the value of the echo will be the value, i.e. {target1:foo1 ,target2:bar2,target3:baz}` and the complite value will be the same just the echo for targets like
target2
echo will be echo from bar2
and for target3
will be echo from baz
– user6124024
Nov 24 '18 at 19:14
yes 1+, thanks a lot. btw if I want to use some key value i mean everthing is the same but just the value of the echo will be the value, i.e. {target1:foo1 ,target2:bar2,target3:baz}` and the complite value will be the same just the echo for targets like
target2
echo will be echo from bar2
and for target3
will be echo from baz
– user6124024
Nov 24 '18 at 19:14
I have edited my answer .
– Shudipta Sharma
Nov 24 '18 at 19:42
I have edited my answer .
– Shudipta Sharma
Nov 24 '18 at 19:42
Amazing thank you very much!
– user6124024
Nov 24 '18 at 19:43
Amazing thank you very much!
– user6124024
Nov 24 '18 at 19:43
Welcome! Best of luck.
– Shudipta Sharma
Nov 24 '18 at 19:45
Welcome! Best of luck.
– Shudipta Sharma
Nov 24 '18 at 19:45
Thank you again :) , I've another question which is related, please see if you can help , stackoverflow.com/questions/53461812/…
– user6124024
Nov 24 '18 at 19:51
Thank you again :) , I've another question which is related, please see if you can help , stackoverflow.com/questions/53461812/…
– user6124024
Nov 24 '18 at 19:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457076%2fgolang-template-for-source-and-target-printing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown