40分钟学 Go 语言高并发:Go语言核心回顾
🕗 发布于 2024-11-21 23:51 golang
开发语言 后端
Day 02 - Go语言核心回顾
1. 接口设计原则
1.1 Go接口设计的基本原则
原则 说明 优点 单一职责 一个接口只负责一个特定的功能 提高代码可维护性 接口隔离 客户端不应依赖它不需要的接口 降低耦合度 组合优于继承 通过组合多个小接口来构建功能 提高灵活性 最小接口 接口应该尽可能小,只包含必要的方法 简化实现难度 依赖倒置 高层模块不应依赖低层模块,都应依赖抽象 提高可扩展性
1.2 接口示例代码
package main
import (
"fmt"
"io"
)
type Reader interface {
Read ( p [ ] byte ) ( n int , err error )
}
type Writer interface {
Write ( p [ ] byte ) ( n int , err error )
}
type ReadWriter interface {
Reader
Writer
}
type DataProcessor interface {
Process ( data [ ] byte ) ( [ ] byte , error )
}
type FileHandler struct {
buffer [ ] byte
}
func ( f * FileHandler) Read ( p [ ] byte ) ( n int , err error ) {
if len ( f. buffer) == 0 {
return 0 , io. EOF
}
n = copy ( p, f. buffer)
f. buffer = f. buffer[ n: ]
return n, nil
}
func ( f * FileHandler) Write ( p [ ] byte ) ( n int , err error ) {
f. buffer = append ( f. buffer, p... )
return len ( p) , nil
}
func ( f * FileHandler) Process ( data [ ] byte ) ( [ ] byte , error ) {
result := make ( [ ] byte , len ( data) )
for i, b := range data {
if b >= 'a' && b <= 'z' {
result[ i] = b - 32
} else {
result[ i] = b
}
}
return result, nil
}
type DataManager struct {
reader Reader
writer Writer
processor DataProcessor
}
func NewDataManager ( rw ReadWriter, p DataProcessor) * DataManager {
return & DataManager{
reader: rw,
writer: rw,
processor: p,
}
}
func ( dm * DataManager) ProcessAndStore ( data [ ] byte ) error {
processed, err := dm. processor. Process ( data)
if err != nil {
return fmt. Errorf ( "process error: %w" , err)
}
_ , err = dm. writer. Write ( processed)
return err
}
func main ( ) {
handler := & FileHandler{ }
manager := NewDataManager ( handler, handler)
data := [ ] byte ( "hello, interface!" )
err := manager. ProcessAndStore ( data)
if err != nil {
fmt. Printf ( "Error: %v\n" , err)
return
}
result := make ( [ ] byte , 100 )
n, err := handler. Read ( result)
if err != nil && err != io. EOF {
fmt. Printf ( "Error: %v\n" , err)
return
}
fmt. Printf ( "Processed data: %s\n" , result[ : n] )
}
2. 结构体内存对齐
2.1 内存对齐规则
规则 说明 基本对齐保证 每个字段的地址必须是其类型大小的整数倍 结构体整体对齐 结构体的总大小必须是其最大字段大小的整数倍 零大小字段 不占用实际内存空间,但可能影响对齐
2.2 字段大小表
类型 大小(字节) 对齐要求 bool 1 1 int8/uint8 1 1 int16/uint16 2 2 int32/uint32/float32 4 4 int64/uint64/float64 8 8 string 16 8 slice 24 8 map 8 8 pointer 8 8
2.3 内存对齐示例
package main
import (
"fmt"
"unsafe"
)
type BadAlignment struct {
a bool
b int64
c int8
d int32
}
type GoodAlignment struct {
b int64
d int32
a bool
c int8
}
func analyzeStruct ( v interface { } ) {
t := fmt. Sprintf ( "%T" , v)
size := unsafe. Sizeof ( v)
fmt. Printf ( "=== %s 分析 ===\n" , t)
fmt. Printf ( "总大小: %d 字节\n" , size)
switch v := v. ( type ) {
case BadAlignment:
fmt. Printf ( "a(bool) 偏移量: %d\n" , unsafe. Offsetof ( v. a) )
fmt. Printf ( "b(int64) 偏移量: %d\n" , unsafe. Offsetof ( v. b) )
fmt. Printf ( "c(int8) 偏移量: %d\n" , unsafe. Offsetof ( v. c) )
fmt. Printf ( "d(int32) 偏移量: %d\n" , unsafe. Offsetof ( v. d) )
case GoodAlignment:
fmt. Printf ( "b(int64) 偏移量: %d\n" , unsafe. Offsetof ( v. b) )
fmt. Printf ( "d(int32) 偏移量: %d\n" , unsafe. Offsetof ( v. d) )
fmt. Printf ( "a(bool) 偏移量: %d\n" , unsafe. Offsetof ( v. a) )
fmt. Printf ( "c(int8) 偏移量: %d\n" , unsafe. Offsetof ( v. c) )
}
}
func main ( ) {
bad := BadAlignment{ }
good := GoodAlignment{ }
analyzeStruct ( bad)
fmt. Println ( )
analyzeStruct ( good)
savings := unsafe. Sizeof ( bad) - unsafe. Sizeof ( good)
fmt. Printf ( "\n内存节省: %d 字节\n" , savings)
}
3. 方法集和接收者
3.1 接收者类型对比
特性 值接收者 指针接收者 方法集 类型T和*T都可以调用 只有类型*T可以调用 数据修改 在方法内部无法修改接收者数据 可以修改接收者数据 内存开销 每次调用会复制数据 只传递指针,开销小 并发安全 相对安全,因为是数据副本 需要考虑并发安全问题
3.2 方法集示例
package main
import (
"fmt"
"sync"
)
type User struct {
ID int
Name string
Age int
mu sync. RWMutex
}
func ( u User) PrintInfo ( ) {
fmt. Printf ( "User %d: %s, %d years old\n" , u. ID, u. Name, u. Age)
}
func ( u * User) UpdateAge ( newAge int ) {
u. mu. Lock ( )
defer u. mu. Unlock ( )
u. Age = newAge
}
func ( u * User) SafeRead ( ) User {
u. mu. RLock ( )
defer u. mu. RUnlock ( )
return * u
}
type UserManager struct {
users map [ int ] * User
mu sync. RWMutex
}
func NewUserManager ( ) * UserManager {
return & UserManager{
users: make ( map [ int ] * User) ,
}
}
func ( um * UserManager) AddUser ( user * User) {
um. mu. Lock ( )
defer um. mu. Unlock ( )
um. users[ user. ID] = user
}
func ( um * UserManager) GetUser ( id int ) ( * User, bool ) {
um. mu. RLock ( )
defer um. mu. RUnlock ( )
user, ok := um. users[ id]
return user, ok
}
func main ( ) {
manager := NewUserManager ( )
user1 := & User{ ID: 1 , Name: "Alice" , Age: 25 }
user2 := & User{ ID: 2 , Name: "Bob" , Age: 30 }
manager. AddUser ( user1)
manager. AddUser ( user2)
user1. PrintInfo ( )
user1. UpdateAge ( 26 )
updatedUser := user1. SafeRead ( )
updatedUser. PrintInfo ( )
var wg sync. WaitGroup
for i := 0 ; i < 10 ; i++ {
wg. Add ( 1 )
go func ( age int ) {
defer wg. Done ( )
user1. UpdateAge ( age)
} ( 20 + i)
}
wg. Wait ( )
finalUser := user1. SafeRead ( )
finalUser. PrintInfo ( )
}
4. 组合与继承
4.1 组合的优势
特性 说明 代码复用 通过组合重用已有功能 松耦合 组件之间独立性强 灵活性 运行时可以动态组合 维护性 易于修改和扩展
4.2 组合示例代码
package main
import (
"fmt"
"sync"
)
type Logger interface {
Log ( message string )
}
type ConsoleLogger struct { }
func ( l * ConsoleLogger) Log ( message string ) {
fmt. Printf ( "Console Log: %s\n" , message)
}
type Storage interface {
Save ( key string , data interface { } ) error
Get ( key string ) ( interface { } , error )
}
type MemoryStorage struct {
data map [ string ] interface { }
mu sync. RWMutex
}
func NewMemoryStorage ( ) * MemoryStorage {
return & MemoryStorage{
data: make ( map [ string ] interface { } ) ,
}
}
func ( m * MemoryStorage) Save ( key string , data interface { } ) error {
m. mu. Lock ( )
defer m. mu. Unlock ( )
m. data[ key] = data
return nil
}
func ( m * MemoryStorage) Get ( key string ) ( interface { } , error ) {
m. mu. RLock ( )
defer m. mu. RUnlock ( )
if data, ok := m. data[ key] ; ok {
return data, nil
}
return nil , fmt. Errorf ( "key %s not found" , key)
}
type Cache struct {
storage Storage
logger Logger
ttl map [ string ] int64
mu sync. RWMutex
}
func NewCache ( storage Storage, logger Logger) * Cache {
return & Cache{
storage: storage,
logger: logger,
ttl: make ( map [ string ] int64 ) ,
}
}
func ( c * Cache) Set ( key string , value interface { } , ttl int64 ) error {
c. mu. Lock ( )
defer c. mu. Unlock ( )
err := c. storage. Save ( key, value)
if err != nil {
c. logger. Log ( fmt. Sprintf ( "Failed to save key %s: %v" , key, err) )
return err
}
c. ttl[ key] = ttl
c. logger. Log ( fmt. Sprintf ( "Successfully saved key %s" , key) )
return nil
}
type Metrics struct {
hits int64
misses int64
mu sync. RWMutex
}
func ( m * Metrics) IncHits ( ) {
m. mu. Lock ( )
defer m. mu. Unlock ( )
m. hits++
}
func ( m * Metrics) IncMisses ( ) {
m. mu. Lock ( )
defer m. mu. Unlock ( )
m. misses++
}
type CacheWithMetrics struct {
* Cache
metrics * Metrics
}
func NewCacheWithMetrics ( storage Storage, logger Logger) * CacheWithMetrics {
return & CacheWithMetrics{
Cache: NewCache ( storage, logger) ,
metrics: & Metrics{ } ,
}
}
func ( c * CacheWithMetrics) Get ( key string ) ( interface { } , error ) {
value, err := c. storage. Get ( key)
if err != nil {
c. metrics. IncMisses ( )
c. logger. Log ( fmt. Sprintf ( "Cache miss for key %s" , key) )
return nil , err
}
c. metrics. IncHits ( )
c. logger. Log ( fmt. Sprintf ( "Cache hit for key %s" , key) )
return value, nil
}
func main ( ) {
logger := & ConsoleLogger{ }
storage := NewMemoryStorage ( )
cache := NewCacheWithMetrics ( storage, logger)
cache. Set ( "user:1" , "Alice" , 3600 )
cache. Set ( "user:2" , "Bob" , 3600 )
v1, _ := cache. Get ( "user:1" )
v2, _ := cache. Get ( "user:2" )
_ , err := cache. Get ( "user:3" )
fmt. Printf ( "Cache hits: %d\n" , cache. metrics. hits)
fmt. Printf ( "Cache misses: %d\n" , cache. metrics. misses)
if err != nil {
fmt. Printf ( "Expected error: %v\n" , err)
}
fmt. Printf ( "Values: %v, %v\n" , v1, v2)
}
5. 实践建议
5.1 接口设计建议
保持接口小巧精简 根据实际需求定义接口 使用组合来构建复杂接口 遵循依赖倒置原则
5.2 内存对齐建议
按大小排列结构体字段 注意填充字节的影响 使用unsafe.Sizeof检查优化效果 考虑缓存行对齐
5.3 方法接收者选择建议
需要修改接收者状态时使用指针接收者 大型结构体推荐使用指针接收者 考虑并发安全性 保持一致性
5.4 组合使用建议
优先使用组合而非继承 通过小接口组合功能 保持组件独立性 考虑运行时组合的灵活性
6. 常见陷阱和问题
接口污染
type Handler interface {
Handle ( )
Initialize ( )
Cleanup ( )
}
type Handler interface {
Handle ( )
}
type Initializer interface {
Initialize ( )
}
type Cleaner interface {
Cleanup ( )
}
内存浪费
type Config struct {
Name string
ID int8
Age int64
Status bool
}
type Config struct {
Age int64
Name string
ID int8
Status bool
}
方法接收者不一致
type Student struct {
Name string
Age int
}
func ( s Student) GetName ( ) string { return s. Name }
func ( s * Student) SetName ( name string ) { s. Name = name }
func ( s Student) GetAge ( ) int { return s. Age }
func ( s * Student) SetAge ( age int ) { s. Age = age }
func ( s * Student) GetName ( ) string { return s. Name }
func ( s * Student) SetName ( name string ) { s. Name = name }
func ( s * Student) GetAge ( ) int { return s. Age }
func ( s * Student) SetAge ( age int ) { s. Age = age }
这些核心概念的深入理解对于编写高质量的Go代码至关重要。建议通过实践和不断重构来加深对这些概念的理解。在后续的高并发编程中,这些基础知识将会反复用到。
怎么样今天的内容还满意吗?再次感谢观众老爷的观看,关注GZH:凡人的AI工具箱,回复666,送您价值199的AI大礼包。最后,祝您早日实现财务自由,还请给个赞,谢谢!
原文地址:https://blog.csdn.net/weixin_40780178/article/details/143905134
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!