This question already has answers here:
Generic Structs with Go
(1 answer)
Go error: cannot use generic type without instantiation
(2 answers)
Closed 10 months ago.
Here is the part of Go code I am reading:
func main() {
var m = map[int]string{1: "2", 2: "4", 4: "8"}
fmt.Println("keys m:", MapKeys(m))
_ = MapKeys[int, string](m)
**lst := List[int]{}**
lst.Push(10)
lst.Push(13)
lst.Push(23)
fmt.Println("list:", lst.GetAll())
}
Anyone can explain what is code below defined ?
lst := List[int]{}
Related
This question already has answers here:
Change values while iterating
(4 answers)
Range references instead values
(8 answers)
increment a struct var in a range loop [duplicate]
(1 answer)
How to return changed values of slice from function? [duplicate]
(2 answers)
Why can't I change the values in a range of type structure?
(2 answers)
Closed 3 days ago.
I am new to Go. I've encountered an issue with Go pointers and don't have an explanation for the unexpected behavior.
I am using using Go 1.20 on RedHat.
Here is my test case:
package main
import (
"fmt"
)
type FileStruct struct {
readyToSend int
}
func sendFile(fileStr *FileStruct) {
fileStr.readyToSend = 2
}
func readFileFeeder(filesArr []FileStruct) {
filesArr[0].readyToSend = 1
for _, file := range filesArr {
fmt.Printf("TYPE=%T\n", &file)
sendFile(&file)
fmt.Printf("After range readyToSend=%v\n", filesArr[0].readyToSend)
}
fmt.Printf("TYPE=%T\n", &filesArr[0])
sendFile(&filesArr[0])
}
func main() {
var file1 *FileStruct = new(FileStruct)
filesArr := make([]FileStruct, 1)
filesArr[0] = *file1
fmt.Printf("Before readyToSend=%v\n", filesArr[0].readyToSend)
readFileFeeder(filesArr)
fmt.Printf("After readyToSend=%v\n", filesArr[0].readyToSend)
}
output:
==\> go run test.go
Before readyToSend=0
TYPE=\*main.FileStruct
After range readyToSend=1
TYPE=\*main.FileStruct
After readyToSend=2
How come when I call sendFile(&file) inside the for loop the value of fileStr.readyToSend doesn't get updated?
When I call sendFile(&filesArr[0]) right after the for loop the value gets updated as expected.
I would expect that fileStr.readyToSend would get updated to 2 in both cases.
How would you update this sample code to make this work?
Thanks
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_
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)
This question already has answers here:
How to convert interface{} to string?
(4 answers)
Closed 1 year ago.
below is my code
func main() {
var a interface{}
b := make(map[string]interface{})
b["mac_addr"] = "fa:16:3e:ba:95:bd"
b["type"] = "fixed"
b["addr"] = "1.1.1.1"
a = b
fmt.Println(a)
}
a gives me output
map[addr:1.1.1.1 mac_addr:fa:16:3e:ba:95:bd type:fixed]
issue is how do I access value of addr from a
use assert
val,ok:=a.(map[string]interface{})
if ok {
fmt.println(val["addr"])
}
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?