惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
Google Developers Blog
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
U
Unit 42
B
Blog RSS Feed
博客园 - 【当耐特】
T
Tailwind CSS Blog
V
V2EX
S
SegmentFault 最新的问题
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Y
Y Combinator Blog
M
MIT News - Artificial intelligence
量子位
aimingoo的专栏
aimingoo的专栏
Stack Overflow Blog
Stack Overflow Blog
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
P
Proofpoint News Feed
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
About on SuperTechFans

ashishb.net

A day in Luxembourg - the richest country in the world I was asked to install malware during a fake interview Book summary: Breakneck - China's quest to engineer the future by Dan Wang Book summary: How to Teach Your Baby to Read Book Summary: The Discontented Little Baby Book by Pamela Douglas Introducing Amazing Sandbox - run third-party tools and AI agents securely on your machine Why software outsourcing gets a bad reputation? Book summary: The Natural Baby Sleep Solution by Polly Moore A day in Antwerp, Belgium Journey of online influencers Two days in Brussels, Belgium Shortcuts - when we love them and when we don't A visit to Rakhigarhi Three days in overhyped Paris Empty Japan, crowded Tokyo The real lock-in in GitHub is not the code, but the stars 11-day Norwegian Breakaway East Caribbean cruise Sanskrit and Sri Lankan Air Force Use REST with Open API The Achilles heel of American capitalism Costa Rica in 4 days At a juice stall in Sri Lanka A short stay at Warsaw, Poland Best practices for using Python & uv inside Docker Two days in Vilnius, Lithuania How IntelliJ IDEs waste disk space Pregnancy Why there aren't many digital nomads from India Two days in Riga, Latvia To keep your machine secure, run third-party tools inside Docker
Generics in Go
Ashish Bhatia · 2023-04-03 · via ashishb.net

Generics in Go were added about a year back in Go 1.18. In my experience they are great and they fix one of the biggest roadblocks in terms of writing reusable code in Go. I’ll illustrate that with an example.

First, do ensure that you have at least Go 1.18

1
2
$ go version
go version go1.19.1 darwin/amd64

Let’s consider a simple case of implementing a Map function in Go that maps an array (well, a slice) to another array (again, actually, a slice).

First, let’s start with a non-generic implementation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import (
  "fmt"
)

func Map(input []int, mapFunc func(int)int) []int {
  output := make([]int, 0, len(input))
  for _, val := range input {
    output = append(output, mapFunc(val))
  }
  return output
}

func main() {
  input := []int {1,2,3}
  squared := func(x int) int {
    return x * x
  }
  output := Map(input, squared)
  fmt.Printf("%+v\n", output)
}

And now let’s generalize the Map function, notice that, in most cases, the caller wouldn’t change at all.

1
2
3
4
5
6
7
8
9
// A is the input type
// B is the output type
func Map[A any,B any](input []A, mapFunc func(A)B) []B {
  output := make([]B, 0, len(input))
  for _, val := range input {
    output = append(output, mapFunc(val))
  }
  return output
}