---
# System prepended metadata

title: Lambda or struct

---

# Lambda or struct

考慮一下版本控制函數

```go
func IsAllowVersion(version uint32) bool 
```

有兩種實作方式

1. lambda
```go
type IsAllowVersion func(version uint32) bool

func NewVersionDetect(allowVersion uint32) (IsAllowVersion, error) {
	return func(version uint32) bool {
		if version < allowVersion {
			return false
		}
		return true
	}, nil
}
```

2. struct
```go
type VersionManager struct {
	allowVersion uint32
}

func NewVersionManager(allowVersion uint32) (VersionManager, error) {
	return VersionManager{
		allowVersion: allowVersion,
	}, nil
}

func (v VersionManager) IsAllowVersion(version uint32) bool {
	if version < v.allowVersion {
		return false
	}
	return true
}
```