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

推荐订阅源

爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
B
Blog
U
Unit 42
B
Blog RSS Feed
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
腾讯CDC
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
博客园 - 聂微东
MyScale Blog
MyScale Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
Engineering at Meta
Engineering at Meta

博客园 - 立志做一个好的程序员

【转】Golang使用时区时候报错 详解 Go 中的 rune 类型 【转】扫盲:Windows桌面应用开发框架:原生、跨平台、云桌面 Go-Zero自定义goctl实战:定制化模板,加速你的微服务开发效率 Go单体服务开发最佳实践 【转】Androidstudio报错Algorithm HmacPBESHA256 not available 【转】linux设置密钥登录(只允许密钥登录) 【转】QPS和并发数的关系 【转】Command Pattern in Go (Golang) [转]Golang Functional Options Pattern 【转】Raft 理论基础 【转】spin lock 和mutex [转]Go sync.Once:简约而不简单的并发利器 [转]Golang atomic.CompareAndSwapInt64()实例讲解 [转]手把手教你如何用golang实现一个timewheel时间轮 [转]Golang线程池实现百万级高并发 [转]Go网络编程 · 一条TCP连接讲透九大知识点 【转】,接上面3篇.Implement Sql Database Driver in 100 Lines of Go 【转】dive into golang database/sql(3)
借助ai来分析代码,理解代码
立志做一个好的程序员 · 2024-04-02 · via 博客园 - 立志做一个好的程序员

这个库不错,学习一下: https://github.com/soheilhy/cmux

800行

67 ./buffer.go
179 ./patricia.go
307 ./cmux.go
18 ./doc.go
267 ./matchers.go
838 total

ChatGPT prompt: Describe multiplexing connections using cmux in golang, with an example combining grpc and https protocols

https://medium.com/@vtalwar.w/chatgpt-prompt-describe-multiplexing-connections-using-cmux-in-golang-with-an-example-combining-1f5b2a3fa585

----------------

Certainly! Here's an example to illustrate the usage of the splitPrefix function in Go:

package main

import (
	"fmt"
)

func splitPrefix(bss [][]byte) (prefix []byte, rest [][]byte) {
	if len(bss) == 0 || len(bss[0]) == 0 {
		return prefix, bss
	}

	if len(bss) == 1 {
		return bss[0], [][]byte{{}}
	}

	for i := 0; ; i++ {
		var cur byte
		eq := true
		for j, b := range bss {
			if len(b) <= i {
				eq = false
				break
			}

			if j == 0 {
				cur = b[i]
				continue
			}

			if cur != b[i] {
				eq = false
				break
			}
		}

		if !eq {
			break
		}

		prefix = append(prefix, cur)
	}

	rest = make([][]byte, 0, len(bss))
	for _, b := range bss {
		rest = append(rest, b[len(prefix):])
	}

	return prefix, rest
}

func main() {
	// Example usage of splitPrefix

	data := [][]byte{
		[]byte("apple"),
		[]byte("app"),
		[]byte("application"),
	}

	prefix, rest := splitPrefix(data)

	fmt.Println("Common Prefix:", string(prefix))
	fmt.Println("Rest:")
	for _, r := range rest {
		fmt.Println(string(r))
	}
}

In this example, we have a collection of byte slices stored in the data variable. Each byte slice represents a word or a string. We want to find the common prefix among these words and separate it from the rest of the data.

We call the splitPrefix function, passing in the data slice. It returns the prefix slice, which contains the common prefix among the words, and the rest slice, which contains the remaining parts of each word after removing the prefix.

In the main function, we print the common prefix and then iterate over the rest slice to print each word without the common prefix.

When you run this program, the output will be:

Common Prefix: app
Rest:
le

In this example, the common prefix among the words "apple", "app", and "application" is "app". The splitPrefix function successfully identifies this common prefix and returns it as the prefix slice. The rest slice contains the remaining parts of each word after removing the prefix, which in this case is "le".

You can use the splitPrefix function in various scenarios where you have a collection of byte slices and you want to identify a common prefix and separate it from the rest of the data, just like in this example.