Advertisement
neunmalelf

dictdb.go

Sep 16th, 2023
1,306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.42 KB | Software | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "strings"
  6. )
  7.  
  8. type Message struct {
  9.     UserID             int
  10.     Text               string
  11.     DateTimeStamp      string
  12.     responseGoFunction string
  13. }
  14.  
  15. func find_words(messages []Message, search_word1, search_word2 string) []Message {
  16.     var result []Message
  17.  
  18.     for _, message := range messages {
  19.         if strings.Contains(message.Text, search_word1) || strings.Contains(message.Text, search_word2) {
  20.             result = append(result, message)
  21.         }
  22.     }
  23.  
  24.     return result
  25. }
  26.  
  27. func main() {
  28.     // IMAGINE you loaded this array at startup or in a repl from a csv file or what ever
  29.     // Format: UserID;Text;DateTimeStampResponseGopFunction, bla bla bla
  30.     messages := []Message{
  31.         {UserID: 1, Text: "Hello world", responseGoFunction: "funA"},
  32.         {UserID: 2, Text: "This is a test", responseGoFunction: "funA"},
  33.         {UserID: 3, Text: "Golang is awesome", responseGoFunction: "funB"},
  34.         {UserID: 4, Text: "Hello, how are you?", responseGoFunction: "funC"},
  35.     }
  36.  
  37.     search_word1 := "Hello"
  38.     search_word2 := "awesome"
  39.  
  40.     result := find_words(messages, search_word1, search_word2)
  41.  
  42.     for _, message := range result {
  43.         // HERE YOU WOULD "ACT" on the responseGoFunction
  44.         fmt.Printf("UserID: %d, Text: %s ResponseGoFunction: %s\n", message.UserID, message.Text, message.responseGoFunction)
  45.         if message.responseGoFunction == "funB" {
  46.             responseText := "Answer: Go away troll!"
  47.             fmt.Printf(responseText)
  48.  
  49.         }
  50.  
  51.     }
  52. }
  53.  
Tags: golang
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement