String manipulation for queuing database

Hello All :smile:
I am receiving a value from get request
r.FormValue("full_name")
which equals for example “John Doe"
I need to manipulate this to use it in full text search query to be like this
”+John +Doe"
so what I did is
_string:= r.FormValue("full_name") __string:= strings.Split(_string, " ") modedString := make([]string, 0) for _, v:= range __string { modedString = append(modedString, string("+") + v } fullNameQuery := strings.Join(modedString, " ")
the problem is it returns
+“John +Doe”

Use regular expressions:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    s := "John Doe"
    r := regexp.MustCompile("(^|\\s)\\b")
    query := r.ReplaceAllString(s, "$1+")
    fmt.Println(query)
}

Try it at https://play.golang.org/p/U0AcH7FVkT.

For an interactive explanation of the regular expression see https://regex101.com/r/GZ8btD/1.

Thanks @lutzhorn , it’s working in the playground but in my program. it returns the same string "John Doe"
btw even my code is working in the playground but not the program.

Can you paste a link to the playground of your code?

Thanks for caring
https://play.golang.org/p/b-a3NyvxeJ

Yes, this playground code works fine.

Can you include the relevant part of your program that does not work`?

The only omitted part is instead of
_string:= "John Doe"
it’s
_string:= r.FormValue(“full_name”)
and full_name = "John Doe"
so what is going on.

Well, since https://golang.org/pkg/net/http/#Request.FormValue returns a simple string, it should not matter if you get the value from

_string := "John Doe"

or

string := r.FormValue("full_name")

if the value of full_name really is John Doe. Are you sure it is?

yes I do because I log it.

Please post your actual code. I think there is some small difference that we can’t see.

1 Like

@nathankerr well this is the actual code really.
the problem is in r.FormValue, it returns string with some kind of hard quotation mark that I have to strip before making any manipulation.
I am still very noob regarding Go, so I am looking for insights from veterans here about how you guys deal with these situations .
and thanks for caring :smile:

What do you mean by that?

Could you please post the exact value of this string? If you just tell us that the value is "John Doe" we must assume that the quotation marks are not part of the string.

The code you posted, https://play.golang.org/p/b-a3NyvxeJ, is not the actual code. There is no call to r.FormValue.

The posted code works. The code you are using does not. Therefore I can’t look at the posted code to see what is going wrong, because it works.

1 Like

well to me it looks like it really return the quotation mark as part of the string, so the only solution for me is to strip them using:
_string:= strings.Trim(r.FormValue("full_name"), "\"")

If this really is the case you should check your HTTP client.

I’ve thrown together a quick HTTP handler that does what you want.

package main

import (
	"io"
	"net/http"
	"regexp"
)

func main() {
	http.HandleFunc("/", handle)
	http.ListenAndServe("localhost:8080", nil)
}

func handle(w http.ResponseWriter, req *http.Request) {
	switch req.Method {
	case http.MethodPost:
		w.Header().Set("Content-Type", "text/plain")
		f := req.FormValue("full_name")
		query := buildQuery(f)
		io.WriteString(w, query)
		io.WriteString(w, "\n")
	default:
		w.WriteHeader(http.StatusMethodNotAllowed)
	}
}

func buildQuery(s string) string {
	r := regexp.MustCompile("(^|\\s)\\b")
	query := r.ReplaceAllString(s, "$1+")
	return query
}

Call it using curl like this:

$ curl -X POST -F full_name="Lutz Horn" http://localhost:8080/

And the body of the HTTP response is

+Lutz +Horn

So it is not the Go server code but your HTTP client that is not working.

I am really grateful for all this good code.
regarding the client, I am using the browser
http://localhost:1234/?full_name="John Doe"

Why do you put the quotation marks into the query parameter value? Write ?full_name=John Doe if that is what you mean.

See https://en.wikipedia.org/wiki/Query_string for details.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.