What does the .(Cat) on line 21 mean? [duplicate] - go

This question already has answers here:
What is the meaning of "dot parenthesis" syntax? [duplicate]
(1 answer)
What exactly does .(data_type) method called/do?
(2 answers)
Is this casting in golang?
(1 answer)
What is this "err.(*exec.ExitError)" thing in Go code? [duplicate]
(2 answers)
Explain Type Assertions in Go
(3 answers)
Closed 6 months ago.
This is code from Golang Tutorial : Go Full Course at 2:37:32
I cannot understand what he mean when using .(Cat) on kitty
Is he kind of type casting or something on the kitty interface to Cat type?(IDK what I am talking, please help)
Please share a link to the documentation if possible
var kitty2 Cat = kitty.(Cat)
package main
type Cat string
type Animal interface {
happy() string
sad() string
}
func (c Cat) happy() string {
return "haha"
}
func (c Cat) sad() string {
return ":("
}
func main() {
var kitty Animal
kitty = Cat("kitty")
var kitty2 Cat = kitty.(Cat)
}

It is an attempt to type cast. Take a look at this lesson in the tour
If they had done the following then you can use to variable ok to check if the type casting worked, here ok is of type bool
kitty, ok := kitty.(Cat)

Related

what's the meaning of global variable _ which convers nil to an interface [duplicate]

This question already has answers here:
What does an underscore and interface name after keyword var mean?
(2 answers)
Meaning of underscore (blank identifier) in Go [duplicate]
(5 answers)
Closed 6 months ago.
I am trying to understand the effect of global variable _ from graph's source code like the belowing code, but at last I can't figure out what's the meaning.
type variable_ interface {
cin()
}
type imple struct {
}
func (i *imple) cin() {
fmt.Println("cout")
}
var (
_ = variable_((*imple)(nil))
)
_ always means ignore in GO. the purpose of _ = variable_((*imple)(nil)) is to check at compile time if *impl implements variable_

Method prints correct data but returns unedited data in Go [duplicate]

This question already has answers here:
Struct field reverts [duplicate]
(1 answer)
How to set and get fields in struct's method
(3 answers)
Assign a new value to a struct field
(2 answers)
object variable is not getting updated in Golang [duplicate]
(1 answer)
Unable to set the property of a struct [duplicate]
(1 answer)
Closed 1 year ago.
This is my second day of learning Go and I'm trying to better understand structs and methods, so apologies in advance if this questions is a bit basic. Why does this program print the edited data inside the method, but by the time it gets back to main, what's printed is the original data passed to the struct?
package main
import ( "fmt" )
type Person struct{
firstName string
lastName string
}
func (p Person) updateName(newName string) string{
p.firstName = newName
fmt.Println(p.firstName, p.lastName) //prints "Jane Doe"
return newName + " " + p.lastName
}
func main() {
p := Person{"John", "Doe"}
p.updateName("Jane")
fmt.Println(p) //prints "{John Doe}"
}
Thanks in advance!

why cannot use (type func(string)) as type func(interface{}) in assignment [duplicate]

This question already has answers here:
Type func with interface parameter incompatible error
(1 answer)
Func with interface argument not equals to func with string argument. Why?
(1 answer)
Go function types that return structs being used with interfaces
(2 answers)
Passing an arbitrary function as a parameter in Go
(4 answers)
How to convert from `func() *int` to `func() interface{}`? [duplicate]
(1 answer)
Closed 8 months ago.
Please first have a look at the code below.
package main
import "fmt"
type InterfaceFunc func(interface{})
type StringFunc func(string)
func stringFunc(s string) {
fmt.Printf("%v", s)
}
func interfaceFunc(i interface{}) {
fmt.Printf("%v", i)
}
func main() {
var i = interfaceFunc
var s = stringFunc
i = s // I would like someone to explain why this can't be done exactly.
}
Run at https://play.golang.org/p/16cE4O3eb95
Why an InterfaceFunc can't hold a StringFunc while an interface{} can hold a string.
You can not do s = i or i = s, and the reason is both functions are of different type (different signatures), you can not just assign one type with another in golang.
Also type InterfaceFunc func(interface{}) type StringFunc func(string) are sitting there doing nothing.

What does "i.(string)" actually mean in golang syntax? [duplicate]

This question already has answers here:
Is this casting in golang?
(1 answer)
What is the meaning of "dot parenthesis" syntax? [duplicate]
(1 answer)
Closed 4 years ago.
I recently started looking for functional go examples and I found this function:
mapper := func (i interface{}) interface{} {
return strings.ToUpper(i.(string))
}
Map(mapper, New(“milu”, “rantanplan”))
//[“MILU”, “RANTANPLAN”]
Now in this function, as you can see the return value of mapper is:
strings.ToUpper(i.(string)).
But, what does this i.(string) syntax mean? I tried searching, but didn't find anything particularly useful.
i.(string) casts (or attempts at least) i (type interface{}) to type string. I say attempts because say i is an int instead, this will panic. If that doesn't sound great to you, then you could change the syntax to
x, ok := i.(string)
In this case if i is not a string, then ok will be false and the code won't panic.
i.(string) means converting i(interface{} type) to string type.

What do you call (*receiverType).method(receiver)? [duplicate]

This question already has answers here:
Pass method argument to function
(3 answers)
golang function alias on method receiver
(3 answers)
Closed 9 months ago.
I recently learned that you can invoke a receiver's method by doing (*receiverType).method(receiver), where the first parameter is always the receiver itself.
func main() {
c := &Cool{}
c.say("Cool!")
(*Cool).say(c, "Hot!") // <-- this
}
type Cool struct {
}
func (it *Cool) say(s string) {
fmt.Println(s)
}
https://play.golang.org/p/vVjr42ceqA
Is there a name for this kind of syntax? Why does this compile?

Resources