Selector

In order to be able to interpret the commands correctly, the message needs to be split into arguments. This is the same logic that the operating system applies to argument passed to a process. The strings.Split function does the trick, by specifying a space as a second argument and breaking the string into words, as shown in the following code:

args := strings.Split(string(s.Bytes()), " ")
cmd := args[0]
args = args[1:]

It's possible to execute any sort of check on cmd, such as the following switch statement:

switch cmd {
case "exit":
return
case "someCommand":
someCommand(w, args)
case "anotherCommand":
anotherCommand(w, args)
}

This allows the user to add a new command by defining a function and adding a new case to the switch statement.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.15.99