Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to remove stop words from a string in Go

2 Answers

0 votes
package main

import (
    "fmt"
    "strings"
)

// A stopwords list is a collection of commonly used words in a language
// that are often removed during text processing tasks.

var stopWords = map[string]struct{}{
    "i": {}, "me": {}, "my": {}, "myself": {}, "we": {}, "our": {}, "ours": {}, "ourselves": {}, "you": {}, "your": {},
    "yours": {}, "yourself": {}, "yourselves": {}, "he": {}, "him": {}, "his": {}, "himself": {}, "she": {}, "her": {},
    "hers": {}, "herself": {}, "it": {}, "its": {}, "itself": {}, "they": {}, "them": {}, "their": {}, "theirs": {},
    "themselves": {}, "what": {}, "which": {}, "who": {}, "whom": {}, "this": {}, "that": {}, "these": {}, "those": {},
    "am": {}, "is": {}, "are": {}, "was": {}, "were": {}, "be": {}, "been": {}, "being": {}, "have": {}, "has": {},
    "had": {}, "having": {}, "do": {}, "does": {}, "did": {}, "doing": {}, "a": {}, "an": {}, "the": {}, "and": {},
    "but": {}, "if": {}, "or": {}, "because": {}, "as": {}, "until": {}, "while": {}, "of": {}, "at": {}, "by": {},
    "for": {}, "with": {}, "about": {}, "against": {}, "between": {}, "into": {}, "through": {}, "to": {}, "from": {},
    "in": {}, "out": {}, "on": {}, "off": {}, "over": {}, "further": {}, "then": {}, "here": {}, "there": {},
    "when": {}, "where": {}, "why": {}, "how": {}, "all": {}, "any": {}, "both": {}, "each": {}, "few": {}, "more": {},
    "most": {}, "other": {}, "some": {}, "such": {}, "no": {}, "nor": {}, "not": {}, "only": {}, "own": {}, "so": {},
    "than": {}, "too": {}, "very": {}, "can": {}, "will": {}, "just": {}, "don": {}, "should": {}, "now": {},
}

func splitWords(input string) []string {
    return strings.Fields(strings.TrimSpace(input))
}

func removeStopWords(words []string) string {
    var filtered []string
    for _, word := range words {
        if _, isStop := stopWords[word]; !isStop {
            filtered = append(filtered, word)
        }
    }
    return strings.Join(filtered, " ")
}

func main() {
    input := "a go and java to python a we if c# then a and aa"
    fmt.Println("Original:", input)

    words := splitWords(input)
    filtered := removeStopWords(words)

    fmt.Println("Filtered:", filtered)
}



/*
run:

Original: a go and java to python a we if c# then a and aa
Filtered: go java python c# aa

*/

 



answered Jul 18, 2025 by avibootz
0 votes
package main

import (
    "fmt"
    "strings"
)

// A stopwords list is a collection of commonly used words in a language
// that are often removed during text processing tasks.

var stopWords = []string{
    "i", "me", "my", "myself", "we", "our", "ours", "ourselves", "you", "your",
    "yours", "yourself", "yourselves", "he", "him", "his", "himself", "she", "her",
    "hers", "herself", "it", "its", "itself", "they", "them", "their", "theirs",
    "themselves", "what", "which", "who", "whom", "this", "that", "these", "those",
    "am", "is", "are", "was", "were", "be", "been", "being", "have", "has", "had",
    "having", "do", "does", "did", "doing", "a", "an", "the", "and", "but", "if", "or",
    "because", "as", "until", "while", "of", "at", "by", "for", "with", "about", "against",
    "between", "into", "through", "to", "from", "in", "out", "on", "off", "over", "further",
    "then", "here", "there", "when", "where", "why", "how", "all", "any", "both", "each",
    "few", "more", "most", "other", "some", "such", "no", "nor", "not", "only", "own",
    "so", "than", "too", "very", "can", "will", "just", "don", "should", "now",
}

func isStopWord(word string) bool {
    for _, stop := range stopWords {
        if word == stop {
            return true
        }
    }
    
    return false
}

func splitWords(input string) []string {
    return strings.Fields(strings.TrimSpace(input))
}

func removeStopWords(words []string) string {
    var filtered []string
    
    for _, word := range words {
        if !isStopWord(word) {
            filtered = append(filtered, word)
        }
    }
    
    return strings.Join(filtered, " ")
}

func main() {
    input := "a go and java to python a we if c# then a and aa"
    fmt.Println("Original:", input)

    words := splitWords(input)
    filtered := removeStopWords(words)

    fmt.Println("Filtered:", filtered)
}



/*
run:

Original: a go and java to python a we if c# then a and aa
Filtered: go java python c# aa

*/

 



answered Jul 18, 2025 by avibootz
...