Longest Word in Go

Published on 19 March 2023 (Updated: 19 March 2023)

Welcome to the Longest Word in Go page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

package main

import (

const errorMessage = "Usage: please provide a string"

// longestWordLength returns the length of the longest word
// in a string. The words in the string should be separated
// by spaces, tabs, newlines, and/or carriage returns.
func longestWordLength(str string) int {
	words := strings.FieldsFunc(str, isLimitedWhitespace)
	return longestStringLength(words)

// isLimitedWhitespace returns whether a rune is one of four
// whitespace runes: ' ', '\t', '\n', '\r'
// Note that this is NOT equivalent to unicode.IsSpace which
// includes addtional whitespace runes
func isLimitedWhitespace(r rune) bool {
	return strings.ContainsRune(" \t\n\r", r)

// longestStringLength returns the length of the longest string
// in the slice
func longestStringLength(strs []string) (longest int) {
	for _, str := range strs {
		if len(str) > longest {
			longest = len(str)

func main() {
	if len(os.Args) < 2 || len(os.Args[1]) == 0 {
	} else {

Longest Word in Go was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.