Interface...https://play.golang.org/p/RKezV69HjPe

https://play.golang.org/p/RKezV69HjPe

func bar(h human) {
switch h.(type) {
case person:
fmt.Println(“I was passed into barrrrrr”, h.(person).first)
case secretAgent:
fmt.Println(“I was passed into barrrrrr”, h.(secretAgent).first)
}
fmt.Println(“I was passed into bar”, h)
}

In this function, there is no receiver, correct?
Is (h human) a parameter?
Is there any reason why case secretAgent is executed before case person?

It seems in the execution ( not sure that is the right word), or when I run it, there should be a line that says “I am Dr. Yes - the person speak” because of “func (p person) speak() {
fmt.Println(“I am”, p.first, p.last, " - the person speak”)"

// conversion
var x hotdog = 42
fmt.Println(x)
fmt.Printf("%T\n", x)
var y int
y = int(x)
fmt.Println(y)
fmt.Printf("%T\n", y)

Language Specification
Conversion = Type “(” Expression [ “,” ] “)” .

I don’t see that his code follows the format in the Language Specification

What part of the code results in “main.hotdog”, specifically, “main”

Why does “fmt.Println(y)” result in “42” when it says nothing about “hotdog”

Could you please use fenced code blocks to highlight your code?

Its hard to follow your examples as they are right now.

Yes, there is none. Also if there were, its called “method” then.

Yes.

None of the branches is “executed” before the other. Gos switch constructs execute always a single branch only (unless you use fallthrough).

When you run the program though, and see it as a whole, and not only that single function, then of course the secretAgent branch is choosen the first 2 calls, as you pass in secretAgents, but to be honest, how do you even tell the difference? Both branches would print the same.

But you never call p1.speak(), so you never instruct your program to actually print that.

The expression int(x) converts the value of xto an int. int is the non-terminal Type, x is expression. Of course () is to see literally.

This one: fmt.Printf("%T\n", x). main.hotdog is the qualified name of your type.

It would be easier for you to recognize which output belongs to which line, if you were using some label:

fmt.Printf("Type of x: %T\n", x)

Because y has the value 42, not hotdog.

Like this?

https://play.golang.org/p/rZH2Efbpot 

Totally new to me. First time I ever heard of it.

Can’t click on it this way

Its meant for code not for links.

Hmmm. I’ll try to remember that. I need practice.

But you never call p1.speak() , so you never instruct your program to actually print that.

I don’t? Don’t I here?

fmt.Println("I am", p.first, p.last, " - the person speak")

Thank-you

And you can use > at the beginning of a line to mark a quote as a quote.

That’s the body of the method, but you are not calling the method. There is nowhere a p1.speak() anywhere in your code.

1 Like

That’s the body of the method, but you are not calling the method

Why would the programmer even include that then?

Because whoever wrote the code wanted person to be human and human requires the implementor to have a speak() method.

You couldn’t pass a person to bar() if it didn’t have that method.

1 Like

Thanks. I juste restudied the code, and I understand it much better. Thanks for your help

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.