
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
github.com/huynhbaoking112/System_design_Golang
Trong Go, interface là một tập hợp các phương thức mà một kiểu dữ liệu (struct) có thể triển khai. Interface giúp đạt được tính đa hình (polymorphism) bằng cách cho phép các kiểu dữ liệu khác nhau thực thi cùng một tập hợp phương thức.
Shape
package main
import (
"fmt"
)
// Định nghĩa interface Shape
type Shape interface {
Area() float64
}
// Struct Circle
type Circle struct {
Radius float64
}
// Struct Rectangle
type Rectangle struct {
Width, Height float64
}
// Implement phương thức Area() cho Circle
func (c Circle) Area() float64 {
return 3.14 * c.Radius * c.Radius
}
// Implement phương thức Area() cho Rectangle
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}
func main() {
// Khai báo một biến kiểu Shape
var s Shape
// Gán một Circle vào s
s = Circle{Radius: 5}
fmt.Println("Circle Area:", s.Area())
// Gán một Rectangle vào s
s = Rectangle{Width: 4, Height: 6}
fmt.Println("Rectangle Area:", s.Area())
}
Shape
có một phương thức Area() float64
.Circle
và Rectangle
đều có phương thức Area()
, vì vậy chúng triển khai interface Shape
.s
có kiểu Shape
, nhưng có thể chứa bất kỳ struct nào triển khai Shape
.s.Area()
, Go sẽ gọi đúng phương thức dựa trên kiểu thực tế (Circle
hoặc Rectangle
).interface{}
)Trong Go, interface{}
là một interface rỗng, có thể chứa bất kỳ kiểu dữ liệu nào.
func PrintValue(value interface{}) {
fmt.Println("Value:", value)
}
func main() {
PrintValue(42) // int
PrintValue("Hello") // string
PrintValue(3.14) // float64
}
interface{}
hữu ích khi bạn muốn viết hàm có thể nhận bất kỳ kiểu dữ liệu nào.type assertion
Khi sử dụng interface rỗng (interface{}
), bạn có thể kiểm tra kiểu dữ liệu thực tế bằng type assertion
.
func CheckType(i interface{}) {
switch v := i.(type) {
case int:
fmt.Println("Integer:", v)
case string:
fmt.Println("String:", v)
default:
fmt.Println("Unknown type")
}
}
func main() {
CheckType(100)
CheckType("GoLang")
CheckType(3.14)
}
✅ Interface trong Go giúp định nghĩa tập hợp phương thức mà một kiểu dữ liệu có thể triển khai.
✅ Interface hỗ trợ đa hình bằng cách cho phép một biến có thể chứa nhiều kiểu khác nhau.
✅ interface{}
là interface đặc biệt có thể chứa mọi kiểu dữ liệu.
✅ Có thể dùng type assertion để kiểm tra kiểu thực tế khi dùng interface.
⏩ Go sử dụng interface để viết code linh hoạt và dễ mở rộng hơn! 🚀
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.