From 30588fb0c995dd7ea49d5e78632c0529f26bacc3 Mon Sep 17 00:00:00 2001 From: Sophie514 Date: Thu, 18 Jul 2024 17:08:40 +0800 Subject: [PATCH] Translate to Vietnamese: Generic Behavior Constraints --- .../vie/generics-behavior-constraints.article | 85 +++++++++---------- 1 file changed, 40 insertions(+), 45 deletions(-) diff --git a/_content/tour/vie/generics-behavior-constraints.article b/_content/tour/vie/generics-behavior-constraints.article index e90152df..c8dbd679 100644 --- a/_content/tour/vie/generics-behavior-constraints.article +++ b/_content/tour/vie/generics-behavior-constraints.article @@ -1,35 +1,35 @@ -Behavior As Constraint -Every generic type requires a constraint to be declared so the compiler knows what concrete type substitutions it can accept or reject at compile time. +Hành vi như là Ràng buộc +Mỗi kiểu generic yêu cầu phải khai báo một ràng buộc để trình biên dịch biết những kiểu thay thế cụ thể nào nó có thể chấp nhận hoặc từ chối tại thời điểm biên dịch. * Generics - Behavior As Constraint -Every generic type requires a constraint to be declared so the compiler knows what -concrete type substitutions it can accept or reject at compile time. This is required -even if there is no real constraint on what the generic type can be, hence the -predeclared constraint identifier any. +Mỗi kiểu generic yêu cầu phải khai báo một ràng buộc để trình biên dịch biết +những kiểu thay thế cụ thể nào nó có thể chấp nhận hoặc từ chối tại thời điểm biên dịch. +Việc này bắt buộc ngay cả khi không có ràng buộc thực sự nào về kiểu generic có thể là gì, +do đó phải khai báo trước định danh ràng buộc any. ** Video -Watch the talk I gave about Generics which walks you through all the -examples in this section of the Tour. +Hãy xem video của tôi về Generics, nó sẽ hướng dẫn bạn tất cả những +ví dụ trong phần này của Tour. .html generics-video.html ** Code Review -- *Example* *1*: Concrete stringify function -- *Example* *2*: Type assertion stringify function -- *Example* *3*: Interface stringify function -- *Example* *4*: Generic stringify function +- *Example* *1*: Hàm stringify cụ thể +- *Example* *2*: Type assertion hàm stringify +- *Example* *3*: Interface hàm stringify +- *Example* *4*: Hàm generic stringify .play generics/behavior-constraints/example1.go .play generics/behavior-constraints/example2.go .play generics/behavior-constraints/example3.go .play generics/behavior-constraints/example4.go -** Explained +** Giải thích -Interesting enough, the concept of a constraint already exists in the language. +Thật thú vị, khái niệm về ràng buộc đã tồn tại sẵn trong ngôn ngữ. type User struct { name string @@ -51,25 +51,21 @@ Interesting enough, the concept of a constraint already exists in the language. s.String() } -The code defines a concrete type named User and implements a method named -String that returns the user’s name. Then an interface type is declared named -Stringer, which declares one act of behavior String, which returns a string. Thanks -to the method declared for User, you can say that the concrete type User implements -the Stringer interface using value semantics. +Code này xác định một kiểu cụ thể có tên là User và thực hiện một phương thức có tên là +String trả về tên của người dùng. Sau đó một kiểu interface được khai báo có tên Stringer, +khai báo một hành vi String, trả về một chuỗi. Cảm ơn phương thức được khai báo cho User, +bạn có thể cho rằng kiểu cụ thể mà User thực hiện interface Stringer sử dụng ngữ nghĩa có giá trị. -The Concrete function is just that, a function that accepts concrete data based on -what it is. The Polymorphic is just that as well, a function that accepts concrete data -based on what it can do. This is the primary difference between a concrete and -polymorphic function. One is limited to one type of data, the other isn’t. However, -there is a constraint on what concrete data can be passed into the polymorphic -function. +Hàm Concrete chỉ là một hàm chấp nhận dữ liệu cụ thể dựa trên nó là gì. Hàm Polymorphic cũng chỉ như vậy, +một hàm chấp nhận dữ liệu cụ thể dựa trên những gì nó có thể làm. Đây là sự khác biệt chính +giữa chức năng concrete và polymorphic. Một cái được giới hạn ở một kiểu dữ liệu, cái còn lại thì không. +Tuy nhiên, có một ràng buộc về dữ liệu concrete nào có thể được chuyển vào hàm polymorphic. -The Stringer interface defines that constraint by declaring a method set of behavior -that concrete data must be able to exhibit. When applied as the input type, the -compiler can guarantee the behavioral constraint is met every time the function is -called. +Interface Stringer xác định ràng buộc đó bằng cách khai báo một tập hợp những hành vi +mà dữ liệu cụ thể đó phải có khả năng thể hiện được. Khi được áp dụng làm kiểu đầu vào, +trình biên dịch có thể đảm bảo ràng buộc về hành vi được đáp ứng mỗi khi hàm được gọi. -There are generic functions that will require the same type of behavioral constraint. +Có những hàm generic sẽ yêu cầu cùng kiểu ràng buộc về hành vi. func stringify[T fmt.Stringer](slice []T) []string { ret := make([]string, 0, len(slice)) @@ -81,27 +77,26 @@ There are generic functions that will require the same type of behavioral constr return ret } -Here is the generic function stringify. It accepts a slice of some type T and returns a -slice of string values that contain a stringified version of each value from the input -collection. The key to making this function work is the method call to String against -each value of type T. +Đây là hàm generic stringify (xâu chuỗi). Nó chấp nhận một slide thuộc kiểu T nào đó và trả về một +slice các giá trị chuỗi, chứa phiên bản được xâu chuỗi của mỗi giá trị từ bộ sưu tập đầu vào. +Chìa khóa để làm cho hàm này hoạt động là lệnh gọi phương thức String cho mỗi giá trị của kiểu T. -The problem is that the compiler needs to know and verify that values of type T do -have a method named String. When the generic type T is declared, the fmt.Stringer -interface is provided as the constraint. The compiler now knows to check any type -substitution and data being passed into the function for this method set of behavior. +Vấn đề là trình biên dịch cần biết và xác minh rằng các giá trị kiểu T có +một phương thức tên String. Khi kiểu generic T được khai báo, interface fmt.Stringer +được cung cấp như là một ràng buộc. Trình biên dịch bấy giờ biết rằng kiểm tra bất kỳ kiểu +thay thế nào và dữ liệu được truyền vào hàm cho tập hợp phương thức của hành vi này. -This is excellent because the interface is being used again for the same purpose and -the language doesn’t need a new keyword. +Điều này thật tuyệt vời vì interface đang được sử dụng lại cho cùng một mục đích và +ngôn ngữ không cần thêm từ khóa mới. -* Exercises +* Bài tập -Use the template as a starting point to complete the exercises. A possible solution is provided. +Sử dụng bài mẫu để bắt đầu giải quyết các bài tập. Một câu trả lời mẫu đã được cung cấp. -** Exercise 1 +** Bài tập 1 -Implement a generic function named marshal that can marshal JSON but only -accepts values that implement the json.Marshaler interface. +Triển khai một hàm generic tên là marshal có thể sắp xếp JSON nhưng chỉ +chấp nhận các giá trị implement interface json.Marshaler. .play generics/behavior-constraints/exercise1.go .play generics/behavior-constraints/answer1.go