Issue with creating a byte object [duplicate] - go

This question already has answers here:
how to put a backquote in a backquoted string?
(3 answers)
Closed 2 years ago.
I am trying to initialize a JSON object as a byte in go lang. Here, I am attaching two exmples
var countryRegionData = []byte(`{"name": "srinivas"}`)
var countryRegionData = []byte(`{"name": "srini`vas"}`)
In the first initilization there is no issue, all working as expected.
In the second initialization if you see there is ` between i and v. I have some requirement like this. How to achieve?

A backtick cannot appear in a raw string literal. You can write something like this:
var countryRegionData = []byte("{\"name\": \"srini`vas\"}")

You cannot use escaping in a raw string literal. Either you have to use double-quoted string:
"{\"name\": \"srini'vas\"}"
Or do something like:
`{"name": "srini`+"`"+"vas"}`

Related

Create variable laravel [duplicate]

This question already has answers here:
Laravel - Pass more than one variable to view
(12 answers)
Closed 2 years ago.
i need to create one variable of text in LARAVEL
if((strlen($tpesquisa ))<4) {
return view('pesquisa');
///////// for example msgm= 'it is necessary to use more than 4 words to start searching';
}
/////////////// for example msgm= 'were found x results';
return view('pesquisa',compact('produtos'));
and in view print the msgm
<p>{{$msgm}}</p>
how create the variable with value text?
and how can I measure the results?
You can just define
$msgm = 'whatever the text'
And then, in the return view you can do:
return view('pesquisa',compact('produtos', 'msgm'));

Request only method returns original request array [duplicate]

This question already has answers here:
Laravel change input value
(7 answers)
Closed 3 years ago.
So I'm manipulating Request and setting an object to new value.
$assignable = ['seats'];
$request->seats = $this->myMethod($request->seats);
var_dump($request->seats); //works
$data = $request->only($assignable);
var_dump($data['seats']); // returns the initial value of 'seats' (without passing through $this->myMethod)
Now I know I could first convert the request object to array and then manipulate the '$data', but the above code is a sample and the real code is much more complicated, it would require to change the whole architecture to do that way.
Has anyone experienced anything like this?
Instead of this:
$request->seats = $this->myMethod($request->seats);
Try this:
$request->merge(['seats' => $this->myMethod($request->seats)]);

How to escape backticks in string literal [duplicate]

This question already has answers here:
How to escape back ticks
(7 answers)
Closed 3 years ago.
I have a slack payload that would format to markdown. And I'm trying to figure out how to preserve the backtick
var jsonStr = []byte(`{
"channel": "#edtest",
"username": "snapshot",
"attachments": [
{
"mkdwn": true,
"text": "`this backtick doesn't work`",
}
]
}`)
if you look at the text field, that backtick won't work
You can't escape backticks. When there's long text like this, one thing you can do is to replace them:
var jsonstr=[]byte(strings.Replace(`{
Some json string with ^backticks^
}`,"^","`",-1))
Another option is to add string segments:
var jsonstr=[]byte(`{
Some json string with `+"`backticks`"+`
}`)

Why does .css work with this Nokogiri object but not XPath? [duplicate]

This question already has answers here:
Selecting a css class with xpath
(6 answers)
Closed 6 years ago.
Why does the CSS selector return the correct info, but the XPath does not?
source = "<hgroup class='page-header channel post-head' data-channel='tech' data-section='sec0=tech&sec1=index&sec2='><h2>Tech</h2></hgroup>"
doc = Nokogiri::HTML(source)
doc.xpath('//hgroup[case_insensitive_equals(#class,"post-head")]//h2', XpathFunctions.new)
=> []
doc.css("hgroup.post-head")[0].css("h2")
=> [#<Nokogiri::XML::Element:0x6c2b824 name="h2" children=[#<Nokogiri::XML::Text:0x6c2b554 "Tech">]>]
Assuming case_insensitive_equals does what its name suggests, it is because the class attribute isn’t equal to post-head (case insensitively or not), but it does contain it. XPath treats class attributes as plain strings, it doesn’t split them and handle the classes individually as CSS does.
A simple XPath that would work would be:
doc.xpath('//hgroup[contains(#class, "post-head")]//h2')
(I’ve removed the custom function, you will need to write your own to do this case insensitively.)
This isn’t quite the same though, as it will also match classes such as not-post-head. A more complete XPath would be something like this:
doc.xpath('//hgroup[contains(concat(" ", normalize-space(#class), " "), " post-head ")]//h2')

How to evaluate a string in its template with given values in ruby

there is a string like this and it is stored in a file
#{date}abcde.doc
I want to be able to read this string and replace #{date} with
Date.today.strftime("%Y%m%d")
Is there any way to parse the template and do the evaluation? Thanks in advance!
Yes, however...
It would be easier if you used hash replacement, like this:
s = "%{date}abcde.doc"
s % { date: Time.now.strftime(etc) }
Or just use ERb.
As-is you're using string interpolation so it would need to be evaled, I think.

Resources