filmov
tv
Golang GENERICS TYPE simple example. Golang quick tips #shorts
Показать описание
Go Programming Language simple example of generics type.
Return integer when we pass a slice of integers.
Return float64 when we pass a slice of float64.
This is a quick golang tips for beginner.
#devlog
Or quickly grab the code:
package main
import "fmt"
func sum[T int | float64](slice []T) T {
var sum T
sum = 0
for _, v := range slice {
sum += v
}
return sum
}
func main() {
intSlice := []int{3, 6, 1, 8}
floatSlice := []float64{3.3, 6.1, 8.5}
intSum := sum(intSlice)
floatSum := sum(floatSlice)
fmt.Println(intSum)
fmt.Println(floatSum)
}
Return integer when we pass a slice of integers.
Return float64 when we pass a slice of float64.
This is a quick golang tips for beginner.
#devlog
Or quickly grab the code:
package main
import "fmt"
func sum[T int | float64](slice []T) T {
var sum T
sum = 0
for _, v := range slice {
sum += v
}
return sum
}
func main() {
intSlice := []int{3, 6, 1, 8}
floatSlice := []float64{3.3, 6.1, 8.5}
intSum := sum(intSlice)
floatSum := sum(floatSlice)
fmt.Println(intSum)
fmt.Println(floatSum)
}
Advanced Golang: Generics Explained
Golang GENERICS TYPE simple example. Golang quick tips #shorts
Golang generics in 6 minutes
Cool Stuff with Golang Generics
Generics in GO #shorts
Learn Go Generics - everything you need to know
Golang Generics is Officially HERE!! (Full Tutorial)
GENERICS type in Go | Golang Generics Type #shorts
Generics in Golang – Full Guide with Examples & Quiz
Golang Generics Are Here! - Golang Beyond the Basics
#40 Golang - Understanding Interfaces and Generics in Go: Real-World Examples
Golang Generics for Beginners (Go generics explained in 2 min)
Golang Generics Quick Example
go generics tutorial - golang finally has generics
Generally About Generics in Go
Golang Generics. Do we ACTUALLY need them!?
GopherCon 2021: Robert Griesemer & Ian Lance Taylor - Generics!
Go 1.18 type parameters | Let's Go generics
Generics in Go 1.18 - Introduced using a practical example
Getting Started with Generics in Go
Go finally got Generics #golang #generics
Simple introduction to Generics in Go
GopherCon Europe 2023: Robert Burke - I'm Excited to Use Generics in Go 1.21
Generics make me scared for the Go community
Комментарии