EASY User Input

In Textio, users can set their profile status to communicate their current activity to those that choose to read it... However, there are some restrictions on what these statuses can contain. Your task is to implement a function that validates a user's status update. The status update cannot be empty and must not exceed 140 characters.

Assignment

Complete the validateStatus function. It should return an error when the status update violates any of the rules:

  • If the status is empty, return an error that reads status cannot be empty.

  • If the status exceeds 140 characters, return an error that says status exceeds 140 characters.

Solution

main.go
package main

import (
	"errors"
)

func validateStatus(status string) error {
	if status == "" {
		var err error = errors.New("status cannot be empty")
		return err
	} else if len(status) > 140 {
		var err error = errors.New("status exceeds 140 characters")
		return err
	}
	return nil
}

Explanation

When the status passes all checks (no errors), you should return nil, because nil means no error occurred.

Here’s the corrected version:

circle-info

When the function succeeds, return nil (no error). When it fails, return an error created with errors.New("message").

🧠 Why this works

Code
Meaning

errors.New("message")

Creates an error value with a custom message

return err

Returns the error (something went wrong)

return nil

Returns no error (everything is fine)

🧩 Example Usage

chevron-rightExample outputshashtag
  • For a valid status:

    • Output: Status is valid!

  • For an empty status:

    • Calling validateStatus("")

    • Output: status cannot be empty

circle-check