Yad Smood
Yad Smood

Reputation: 2932

Does fmt.Println receive any type of argument?

How does fmt.Println work? Why I can pass an int or a string to it?

How can I make the code below work?

package main

import "fmt"

func ln(a interface{}) {
    fmt.Println(a)
}

func main() {
    ln(123)
    ln("test")
}

Upvotes: 0

Views: 89

Answers (1)

peterSO
peterSO

Reputation: 166626

For example,

package main

import "fmt"

func ln(a interface{}) {
    fmt.Println(a)
}

func main() {
    ln(123)
    ln("test")
}

Output:

123
test

func Println

func Println(a ...interface{}) (n int, err error)

The Go fmt package uses the reflect package.

Upvotes: 2

Related Questions