Recap Challenge #1
Lesson 16 of 19 in Coddy's Functions and Pointers in Golang course.
If we need to check if a string starts with specific characters, we can use the HasPrefix function, which will return true if the string starts with the given characters, and false if it doesn't. Here's an example of how this function works:
str := "Gopher"
s := "Go"
b := strings.HasPrefix(str, s)
fmt.Println(b)The HasPrefix function takes two parameters: the string str and the characters that we want to check s.
In our example, it will return:
trueBecause the string "Gopher" starts with the characters "Go".
Challenge
EasyLet's build our HasPrefix function! 🤷
Write a function called hasPrefix that takes two strings and returns a boolean value.
- The first parameter is the string value.
- The second parameter is the substring or the characters that we will check.
- Check if the string starts with the given substring (characters) return true is not return false.
Note that the substring may contain more than one character.
Try it yourself
// Your Code HereAll lessons in Functions and Pointers in Golang
1Introduction
Introduction4Rebuild Go Strings Functions
Strings in GolangJoin FunctionContains FunctionIndex FunctionRecap Challenge #1