how to put a backquote in a backquoted string? - go

is it possible to print back quotes in Go using back quotes : something like this:
package main
import "fmt"
func main() {
fmt.Println(```) // for example I can do it with double quotes "\""
}

package main
import "fmt"
func main() {
// back ` quote
fmt.Println((`back ` + "`" + ` quote`))
}
Raw string literals are character
sequences between back quotes ``.
Within the quotes, any character is
legal except back quote. The value of
a raw string literal is the string
composed of the uninterpreted
characters between the quotes; in
particular, backslashes have no
special meaning and the string may
span multiple lines. String
literals

You can also do it with single quotes:
package main
import "fmt"
func main() {
fmt.Printf("%c\n", '`')
}
https://golang.org/pkg/fmt#Printf

TLDR
fmt.Println("\x60")
\x: Hex see fmt
6016
9610
1408 matches the character ` grave accent

Related

Unmarshal returns blank object due to encoding

I'm attempting to unmarshal a raw json string. There seems to be an error with encoding but I can't quite figure it out.
package main
import (
"encoding/json"
"fmt"
"log"
)
type Foo struct {
Transmission string `json:"transmission"`
Trim string `json:"trim"`
Uuid string `json:"uuid"`
Vin string `json:"vin"`
}
func main() {
var foo Foo
sample := `{
"transmission": "continuously\x20variable\x20automatic",
"trim": "SL",
"uuid" : "6993e4090a0e0ae80c59a76326e360a1",
"vin": "5N1AZ2MH6JN192059"
}`
err := json.Unmarshal([]byte(sample), &foo)
if err != nil {
log.Fatal(err)
}
fmt.Println(foo)
}
2009/11/10 23:00:00 invalid character 'x' in string escape code
It works if transmission entry is removed.
Here is a working playground.
Your input is not valid JSON. The JSON spec states that
All code points may be placed within the quotation marks except for the code points that must be escaped: quotation mark (U+0022), reverse solidus (U+005C), and the control characters U+0000 to U+001F.
Additionally, although there are two-character escape sequences, \x is not one of them, and thus it is being correctly interpreted as an invalid escape sequence by the Go parser. If you want to have a backslash literal in your JSON, it needs to be represented by \\ in the JSON input itself. See a modified version of your example: https://play.golang.org/p/JZdPJGpPR5q
(note that this is not an issue with your Go string literal since you're already using a raw (``) string literal—the JSON itself needs to have two backslashes.)
You can replace \x with \\x using string.Replace() function. Then, Unmarshal the replaced string. Here is a working example.

How to Print ascii text in go like python does

how to print ascii-text in go language like python does
like picture shown below
Using python
Using Golang
The problem is that your text contains backtick (`), which happen to be delimiter character for golang's raw string literal. This situation is comparable to your python code had your text contains 3 consecutive double-quotes, which is the delimiter being used in your python code.
I don't see any quick escape from this situation without modifying your ascii text, as we don't have other options for raw string delimiter in golang like we have in python. You may want to store your ascii text in a text file and read it from there :
import (
....
....
"io/ioutil"
)
func banner() string {
b, err := ioutil.ReadFile("ascii.txt")
if err != nil {
panic(err)
}
fmt.Println(string(b))
}
If you're ok with slight modification to the ascii text source, then you can temporarily use other character that isn't used anywhere else in the ascii text to represent backtick, and then do string replacement to put the actual backtick in place. Or, you can use fmt.Sprintf to supply the problematic backtick :
ascii := fmt.Sprintf(`....%c88b...`, '`')
fmt.Println(ascii)
// output:
// ....`88b...
Yes but you have to split lines with backtick and put them quoted into standard double quote ”.
... +
“888 6(, ` ‘ “ +
...

cmd line parameter string.Contains behaving differently from hardcoded parameter

I'm looking to get some clarification on why these two strings.Contains() calls behave differently.
package main
import (
"strings"
"os"
"errors"
"fmt"
)
func main() {
hardcoded := "col1,col2,col3\nval1,val2,val3"
if strings.Contains(hardcoded, "\n") == false {
panic(errors.New("The hardcoded string should contain a new line"))
}
fmt.Println("New line found in hardcoded string")
if len(os.Args) == 2 {
fmt.Println("parameter:", os.Args[1])
if strings.Contains(os.Args[1], "\n") == false {
panic(errors.New("The parameter string should contain a new line."))
}
fmt.Println("New line found in parameter string")
}
}
If I run this with
go run input-tester.go col1,col2,col3\\nval1,val2,val3
I get the following
New line found in hardcoded string
parameter: col1,col2,col3\nval1,val2,val3
panic: The parameter string should contain a new line.
goroutine 1 [running]:
panic(0x497100, 0xc42000e310)
/usr/local/go/src/runtime/panic.go:500 +0x1a1
main.main()
/home/user/Desktop/input-tester.go:21 +0x343
exit status 2
I can see that the string printed out is the same format as the string that is hardcoded yet the string.Contains() doesn't find the "\n".
I'm guessing this is an oversight on my part. Can anyone explain what I'm missing or misunderstanding?
It behaves differently because in hardcoded \n is considered as new line parameter.
And in command line arguments , argument type is string, where given condition is for "\n" which is considered as new line parameter.
Simply ` \n compaires with two consecutive characters "\" and "n" not with "\n" a new line character.
So for command line arguments use,
if strings.Contains(os.Args[1], `\n`) == false {
panic(errors.New("The parameter string should contain a new line."))
}
Reference : https://golang.org/ref/spec#String_literals
Raw string literals are character sequences between back quotes, as in foo. Within the quotes, any character may appear except back quote. The value of a raw string literal is the string composed of the uninterpreted (implicitly UTF-8-encoded) characters between the quotes; in particular, backslashes have no special meaning and the string may contain newlines.

Unable to use backslash `\` in Golang os.Arg variable

I have made a program in Golang and I am trying to use a file as the first argument when launching my program.
For example: ./goprogram.exe C:\Acidic\image.png
When my program tries to use the os.Arg[1] variable which should be the link to the image.png file, it returns the string without any of the backslashes (C:Acidicimage.png).
How can I use the whole string of an argument without characters being escaped?
I have made a little example:
package main
import (
"fmt"
"os"
)
func main() {
if len(os.Args) >= 2 {
fmt.Println(os.Args[1])
}
}
I run go run args.go C:\Users\image.png and it outputs C:Usersimage.png
Put any argument between quotes:
./goprogram.exe "C:\Acidic\image.png"
The issue appears to be your shell. Try quoting the file path:
./goprogram.exe 'C:\Acidic\image.png' # prevents escape sequence execution
A string fo type C:\Acidic\image.png (with single \'s) is not a valid string in Go (invalid escape sequence \A, etc) and would not even compile.

How to strings.Split on newline?

I'm trying to do the rather simple task of splitting a string by newlines.
This does not work:
temp := strings.Split(result,`\n`)
I also tried ' instead of ` but no luck.
Any ideas?
You have to use "\n".
Splitting on `\n`, searches for an actual \ followed by n in the text, not the newline byte.
playground
For those of us that at times use Windows platform, it can
help remember to use replace before split:
strings.Split(strings.ReplaceAll(windows, "\r\n", "\n"), "\n")
Go Playground
It does not work because you're using backticks:
Raw string literals are character sequences between back quotes ``. Within the quotes, any character is legal except back quote. The value of a raw string literal is the string composed of the uninterpreted (implicitly UTF-8-encoded) characters between the quotes; in particular, backslashes have no special meaning and the string may contain newlines.
Reference: http://golang.org/ref/spec#String_literals
So, when you're doing
strings.Split(result,`\n`)
you're actually splitting using the two consecutive characters "\" and "n", and not the character of line return "\n". To do what you want, simply use "\n" instead of backticks.
Your code doesn't work because you're using backticks instead of double quotes. However, you should be using a bufio.Scanner if you want to support Windows.
import (
"bufio"
"strings"
)
func SplitLines(s string) []string {
var lines []string
sc := bufio.NewScanner(strings.NewReader(s))
for sc.Scan() {
lines = append(lines, sc.Text())
}
return lines
}
Alternatively, you can use strings.FieldsFunc (this approach skips blank lines)
strings.FieldsFunc(s, func(c rune) bool { return c == '\n' || c == '\r' })
import regexp
var lines []string = regexp.MustCompile("\r?\n").Split(inputString, -1)
MustCompile() creates a regular expression that allows to split by both \r\n and \n
Split() performs the split, seconds argument sets maximum number of parts, -1 for unlimited
' doesn't work because it is not a string type, but instead a rune.
temp := strings.Split(result,'\n')
go compiler: cannot use '\u000a' (type rune) as type string in argument to strings.Split
definition: Split(s, sep string) []string

Resources