It should take a message string and messageLength int as inputs.
If the messageLength is within the user's message character limit, return the original message and true (indicating the message can be sent), otherwise, return an empty string and false.
packagemainfunc(u User)SendMessage(messagestring,messageLengthint)(string,bool){inLimit:=messageLength<=u.MessageCharLimitifinLimit==true{returnmessage,inLimit}else{return"",inLimit}}// don't touch below this linetypeUserstruct{NamestringMembership}typeMembershipstruct{TypestringMessageCharLimitint}funcnewUser(namestring,membershipTypestring)User{membership:=Membership{Type:membershipType}ifmembershipType=="premium"{membership.MessageCharLimit=1000}else{membership.Type="standard"membership.MessageCharLimit=100}returnUser{Name:name,Membership:membership}}
Test
1
1. Structs
User represents a person with a name and a membership.
Membership is embedded, meaning User can access its fields directly (like u.MessageCharLimit).
2
2. Membership limits
A standard user can send messages up to 100 characters.
A premium user can send up to 1000 characters.
3
3. Creating a new user
Example:
4
4. SendMessage method
(u User) means this function belongs to the User type.
(string, bool) are the two return values — the message and whether it’s valid.
It compares the message’s length (messageLength) to u.MessageCharLimit.
5
5. Logic
inLimit becomes true if message length is small enough.
If true, send the message.
If false, block it and return an empty string.
Example Usage
Key Takeaways
func (u User) → defines a method attached to the User struct.
Return types (string, bool) → Go supports returning multiple values.
Embedded structs → allow direct field access like u.MessageCharLimit.
Logical check → use <= to compare message length with limit.
---------------------------------
Test Passed:
* user: Syl
* membership type: standard
* message: Hello, Kaladin!
* expected result: Hello, Kaladin!
* expected success: true
* actual result: Hello, Kaladin!
* actual success: true
---------------------------------
Test Passed:
* user: Pattern
* membership type: premium
* message: You are not as good with patterns... You are abstract. You think in lies and tell them to yourselves. That is fascinating, but it is not good for patterns.
* expected result: You are not as good with patterns... You are abstract. You think in lies and tell them to yourselves. That is fascinating, but it is not good for patterns.
* expected success: true
* actual result: You are not as good with patterns... You are abstract. You think in lies and tell them to yourselves. That is fascinating, but it is not good for patterns.
* actual success: true
---------------------------------
Test Passed:
* user: Dalinar
* membership type: standard
* message: I will take responsibility for what I have done. If I must fall, I will rise each time a better man.
* expected result: I will take responsibility for what I have done. If I must fall, I will rise each time a better man.
* expected success: true
* actual result: I will take responsibility for what I have done. If I must fall, I will rise each time a better man.
* actual success: true
---------------------------------
3 passed, 0 failed, 2 skipped
PASS
type User struct {
Name string
Membership
}
type Membership struct {
Type string
MessageCharLimit int
}
func newUser(name string, membershipType string) User
func (u User) SendMessage(message string, messageLength int) (string, bool)
inLimit := messageLength <= u.MessageCharLimit
func main() {
user := newUser("Alice", "standard")
msg, ok := user.SendMessage("Hello, world!", 13)
println(msg, ok) // Output: Hello, world! true
msg2, ok2 := user.SendMessage("This message is way too long to fit inside the limit...", 200)
println(msg2, ok2) // Output: false
}