Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Check if an item exists in a slice in Go (Golang)

Posted on February 29, 2023February 29, 2023 by admin

Let’s see a working code

package main
import "fmt"
func main() {
source := []string{"san", "man", "tan"}
result := find(source, "san")
fmt.Printf("Item san found: %t\n", result)
result = find(source, "can")
fmt.Printf("Item san found: %t\n", result)
}
func find(source []string, value string) bool {
for _, item := range source {
if item == value {
return true
}
}
return false
}

Output:

Item san found: true
Item can found: false

Popular Articles

Golang Comprehensive Tutorial Series

All Design Patterns in Go (Golang)

Slice in golang

Variables in Go (Golang) – Complete Guide

OOP: Inheritance in GOLANG complete guide

Using Context Package in GO (Golang) – Complete Guide

All data types in Golang with examples

Understanding time and date in Go (Golang) – Complete Guide

©2023 Welcome To Golang By Example | Design: Web XP