How to expand variables with fmt.Println() - go

I can't expand variables with fmt.Println().
package main
import "fmt"
func main(){
old := 20
fmt.Println("I'm %g years old.",old)
}
result =>
I'm %g years old.
20

Use Printf not Println. Use %d for old which is type int. Add a newline.
For example,
package main
import "fmt"
func main() {
old := 20
fmt.Printf("I'm %d years old.\n", old)
}
Output:
I'm 20 years old.

As the documentation for fmt.Println states, this function does not support format specifiers. Use fmt.Printf instead.

Related

Convert timestamp as string [duplicate]

This question already has answers here:
Convert time.Time to string
(6 answers)
Closed 2 years ago.
I want to get a timestamp as string. If I use string conversion I got no error but the output is not readable.
Later, I want us it as a part of a filename.
It looks like a question mark for e.g. �
I found some examples like this: https://play.golang.org/p/bq2h3h0YKp
not solves completely me problem. thanks
now := time.Now() // current local time
sec := now.Unix() // number of seconds since January 1, 1970 UTC
fmt.Println(string(sec))
How could I get the timestamp as string?
Something like this works for me
package main
import (
"fmt"
"strconv"
"time"
)
func main() {
now := time.Now()
unix := now.Unix()
fmt.Println(strconv.FormatInt(unix, 10))
}
Here are two examples of how you can convert a unix timestamp to a string.
The first example (s1) uses the strconv package and its function FormatInt. The second example (s2) uses the fmt package (documentation) and its function Sprintf.
Personally, I like the Sprintf option more from an aesthetic point of view. I did not check the performance yet.
package main
import "fmt"
import "time"
import "strconv"
func main() {
t := time.Now().Unix() // t is of type int64
// use strconv and FormatInt with base 10 to convert the int64 to string
s1 := strconv.FormatInt(t, 10)
fmt.Println(s1)
// Use Sprintf to create a string with format:
s2 := fmt.Sprintf("%d", t)
fmt.Println(s2)
}
Golang Playground: https://play.golang.org/p/jk_xHYK_5Vu

Parameter that's a list of interface{} [duplicate]

This question already has an answer here:
Passing interface{} or []interface{} in Golang
(1 answer)
Closed 4 years ago.
I'm trying to create a function that prints out the len of a list passed into it, regardless of the type of the list. My naive way of doing this was:
func printLength(lis []interface{}) {
fmt.Printf("Length: %d", len(lis))
}
However, when trying to use it via
func main() {
strs := []string{"Hello,", "World!"}
printLength(strs)
}
It complains saying
cannot use strs (type []string) as type []interface {} in argument to printLength
But, a string can be used as a interface{}, so why can't a []string be used as a []interface{}?
You can use reflect package - playground
import (
"fmt"
"reflect"
)
func printLength(lis interface{}) {
fmt.Printf("Length: %d", reflect.ValueOf(lis).Len())
}

What is wrong with this go code, and what is os.Stdin? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
1 package main
2
3 import (
4 "bufio"
5 "fmt"
6 "os"
7 )
8
9 func main() {
10 input := bufio.NewScanner(os.Stdin)
11 if input.Scan == 1 {
12 fmt.println("true")
13 }
14 }
I want create something that will ask for user input, then check if that user input = 1
The Scan code documentation says:
//Scan advances the Scanner to the next token, which will then be
//available through the Bytes or Text method. It returns false when the
//scan stops, either by reaching the end of the input or an error.
So you could do something like this:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
input := bufio.NewScanner(os.Stdin)
if input.Scan() && input.Text() == "1" {
fmt.Println("true")
}
}
The os.Stdin is how you make your Scanner get it's input from the stdin.
(https://en.wikipedia.org/wiki/Standard_streams#/media/File:Stdstreams-notitle.svg)
One note, pay attention for uppercase letters for exported functions.
On line 12 you wrote
fmt.println
and it should be
fmt.Println
You should go to
https://tour.golang.org/welcome/1
to get started with golang.

How do I make a GO program wait until there is user input? [duplicate]

This question already has answers here:
How can I read from standard input in the console?
(12 answers)
Closed 7 years ago.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
fmt.Println("insert y value here: ")
input := bufio.NewScanner(os.Stdin)
fmt.Println(input.Text)
}
How do I make the program wait, until the user inputs data?
Scanner isn't really ideal for reading command line input (see the answer HectorJ referenced above), but if you want to make it work, it's a call to Scan() that you're missing (also note that Text() is a method call):
func main() {
fmt.Print("insert y value here: ")
input := bufio.NewScanner(os.Stdin)
input.Scan()
fmt.Println(input.Text())
}

How to get executing code's file's name in go? [duplicate]

This question already has answers here:
Is there a way to get the source code filename and line number in Go?
(1 answer)
Find the path to the executable
(3 answers)
Closed 7 years ago.
Say I have a file:
i_want_this_name.go:
package main
func main(){
filename := some_func() // should be "i_want_this_name"
}
How do I get the executing code's file's name in go?
The name of the command can be found in os.Args[0] as states in the documentation for the os package:
var Args []string
Args hold the command-line arguments, starting with the program name.
To use it, do the following:
package main
import "os"
func main(){
filename := os.Args[0]
}
This should work for you:
package main
import (
"fmt"
"runtime"
)
func main() {
_, fileName, lineNum, _ := runtime.Caller(0)
fmt.Printf("%s: %d\n", fileName, lineNum)
}

Resources