Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
Searching an element of string type in Golang slice
Next article icon

Searching an element of string type in Golang slice

Last Updated : 05 Sep, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice, you can search an element of string type in the given slice of strings with the help of SearchStrings() function. This function searches for the given element in a sorted slice of strings and returns the index of that element if present in the given slice. And if the given element is not available in the slice(it could be len(s_slice)), then it returns the index to insert the element in the slice. The specified slice must be sorted in ascending order. It is defined under the sort package so, you have to import sort package in your program for accessing SearchStrings function. Syntax:
func SearchStrings(s_slice []string, s string64) int
Example 1: C
// Go program to illustrate how to search an // element of string type in the slice of strings package main  import (     "fmt"     "sort" )  // Main function func main() {      // Creating and initializing      // slice of strings     // Using shorthand declaration     slice_1 := []string{"C", "Go", "Java", "C#", "Ruby"}     slice_2 := []string{"GEEKs", "123geeks", "gfg", "GeeksforGeeks"}      var f1, f2, f3 string     f1 = "GEEKs"     f2 = "C"     f3 = "gfg"      // Sorting the given      // slice of strings     sort.Strings(slice_1)     sort.Strings(slice_2)      // Displaying the slices     fmt.Println("Slice 1: ", slice_1)     fmt.Println("Slice 2: ", slice_2)      // Searching a int type element      // in the given slice     // Using SearchStrings function     res1 := sort.SearchStrings(slice_1, f1)     res2 := sort.SearchStrings(slice_2, f2)     res3 := sort.SearchStrings(slice_2, f3)      // Displaying the results     fmt.Println("Result 1: ", res1)     fmt.Println("Result 2: ", res2)     fmt.Println("Result 3: ", res3)  } 
Output:
  Slice 1:  [C C# Go Java Ruby]  Slice 2:  [123geeks GEEKs GeeksforGeeks gfg]  Result 1:  2  Result 2:  1  Result 3:  3  
Example 2: C
// Go program to illustrate how to search an element // of string type in the slice of strings package main  import (     "fmt"     "sort" )  // Main function func main() {      // Creating and searching an element     // in the given slice of strings     // Using SearchStrings function     res1 := sort.SearchStrings([]string{"apple", "banana",                                 "kiwi", "orange"}, "kiwi")          res2 := sort.SearchStrings([]string{"Cat", "Cow",                              "Dog", "Parrot"}, "Cat")      // Displaying the results     fmt.Println("Result 1: ", res1)     fmt.Println("Result 2: ", res2)  } 
Output:
  Result 1:  2  Result 2:  0  

Next Article
Searching an element of string type in Golang slice

A

ankita_saini
Improve
Article Tags :
  • Go Language
  • Golang
  • Golang-Slices

Similar Reads

    Searching an element of float64 type in Golang slice
    In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice, you can search
    3 min read
    Golang | Searching an element of int type in slice of ints
    In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice, you can search
    3 min read
    How to sort a slice of Search in Golang?
    Go language provides inbuilt support implementation of basic constants and run-time reflection to operate sort package. Golang has the ability for functions to run independently of each other. By the help of this function we can easily sort integer and string by importing "sort" package. These funct
    3 min read
    How to Sort a Slice of Strings in Golang?
    In Go, a slice is a flexible data structure that stores a variable-length sequence of elements of the same type. Unlike arrays, slices can dynamically grow and shrink. You cannot mix different types within a slice. Go provides built-in functions for sorting slices, particularly for strings, found in
    2 min read
    Delete Elements in a Slice in Golang
    A Slice in Golang is an array(list of elements of the same type) but it's dynamic i.e. it can adjust its size as per the elements added throughout the program. We can initialize a slice without specifying the initial length or size, this allows us to add elements to it dynamically. So, there might b
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences