I am a beginner in Golang. I wrote a function that will take variable args and pass it to another function that accepts variable args. I used "exec.Command()" for the second one. Here is my program
package main
import "fmt"
import "os/exec"
func execute(command string, parameters ...string) {
cmd := exec.Command(command, parameters...)
fmt.Println("Path =", cmd.Path, "Args =", cmd.Args, "Dir =", cmd.Dir)
out,_ := cmd.Output()
fmt.Println("Output =", out)
}
func main() {
execute("ls", "-l")
}
I expected it to return the list of files in the current directory. Instead I get a strange result
# go build command.go
#./command
Path = /bin/ls Args = [ls -l] Dir =
Output = [116 111 116 97 108 32 57 49 52 52 10 45 114 119 120 114 45 120 114 45 120 32 49 32 114 111 111 116 32 114 111 111 116 32 50 55 53 55 57 53 50 32 78 111 118 32 50 48 32 49 55 58 50 54 32 99 111 109 109 97 110 100 10 45 114 119 45 114 45 45 114 45 45 32 49 32 114 111 111 116 32 114 111 111 116 32 32 32 32 32 51 48 55 32 78 111 118 32 50 48 32 49 55 58 50 54 32 99 111 109 109 97 110 100 46 103 111 10 45 114 119 120 114 45 120 114 45 120 32 49 32 114 111 111 116 32 114 111 111 116 32 51 52 48 49 48 51 50 32 78 111 118 32 50 48 32 49 53 58 52 51 32 110 101 120 117 115 49 48 48 48 118 10 45 114 119 45 114 45 45 114 45 45 32 49 32 114 111 111 116 32 114 111 111 116 32 32 32 32 56 57 48 52 32 78 111 118 32 50 48 32 49 53 58 52 51 32 110 101 120 117 115 49 48 48 48 118 46 103 111 10 45 114 119 120 114 45 120 114 45 120 32 49 32 114 111 111 116 32 114 111 111 116 32 51 49 55 53 52 48 48 32 78 111 118 32 50 48 32 49 55 58 50 52 32 116 101 115 116 110 101 116 10 45 114 119 45 114 45 45 114 45 45 32 49 32 114 111 111 116 32 114 111 111 116 32 32 32 32 32 52 55 56 32 78 111 118 32 50 48 32 49 55 58 50 53 32 116 101 115 116 110 101 116 46 103 111 10]
What am I doing wrong
You are printing a byte slice.
fmt.Println("Output =", out)
Convert it to a string.
fmt.Println("Output =", string(out))
When using the fmt package, the formatting string is very concise:
fmt.Printf("%s\n", out)
Related
I am trying to encode a byte array as Base64 and running into two issues. I can do this with base64.StdEncoding.EncodedLen(text) but I'm worried that's costly, so I wanted to see if I could do it just with len(text). Here is the code (the functions are named "Marshal" because I'm using them as a field converter during JSON Marshaling):
package main
import (
"crypto/rand"
"encoding/base64"
"fmt"
)
func main() {
b := make([]byte, 60)
_, _ = rand.Read(b)
// Marshal Create Dst Buffer
MarshalTextBuffer(b)
// Marshal Convert to String
MarshalTextStringWithBufferLen(b)
// Marshal Convert to String
MarshalTextStringWithDecodedLen(b)
}
func MarshalTextBuffer(text []byte) error {
ba := base64.StdEncoding.EncodeToString(text)
fmt.Println(ba)
return nil
}
func MarshalTextStringWithBufferLen(text []byte) error {
ba := make([]byte, len(text)+30) // Why does len(text) not suffice? Temporarily using '30' for now, just so it doesn't overrun.
base64.StdEncoding.Encode(ba, text)
fmt.Println(ba)
return nil
}
func MarshalTextStringWithDecodedLen(text []byte) error {
ba := make([]byte, base64.StdEncoding.EncodedLen(len(text)))
base64.StdEncoding.Encode(ba, text)
fmt.Println(ba)
return nil
}
Here's the output:
IL5CW8T9WSgwU5Hyi9JsLLkU/EcydY6pG2fgLQJsMaXgxhSh74RTagzr6b9yDeZ8CP4Azc8xqq5/+Cgk
[73 76 53 67 87 56 84 57 87 83 103 119 85 53 72 121 105 57 74 115 76 76 107 85 47 69 99 121 100 89 54 112 71 50 102 103 76 81 74 115 77 97 88 103 120 104 83 104 55 52 82 84 97 103 122 114 54 98 57 121 68 101 90 56 67 80 52 65 122 99 56 120 113 113 53 47 43 67 103 107 0 0 0 0 0 0 0 0 0 0]
[73 76 53 67 87 56 84 57 87 83 103 119 85 53 72 121 105 57 74 115 76 76 107 85 47 69 99 121 100 89 54 112 71 50 102 103 76 81 74 115 77 97 88 103 120 104 83 104 55 52 82 84 97 103 122 114 54 98 57 121 68 101 90 56 67 80 52 65 122 99 56 120 113 113 53 47 43 67 103 107]
Why does the middle one MarshalTextStringWithBufferLen require extra padding?
Is base64.StdEncoding.EncodedLen a costly function (e.g. I can solve it with the bottom function, but I worry about the cost).
Base-64 encoding stores binary data (8 bits per byte) as text (using 6 bits per byte), so every 3 bytes is encoded as 4 bytes (3x8 = 4x6). So len(text) + 30 in your code is wrong, and should be len(text)*4/3 (if len(text) is divisible by 3) but to make for readability and to avoid bugs you should be using base64.StdEncoding.EncodedLen() to get the length.
If you look at the code for base64.StdEncoding.EncodedLen you will see that it is as fast as doing the calcs yourself (esp. as it will be in-lined).
I read geojson with
json, err := ioutil.ReadFile(file)
Then I Print result and see this
[123 32 34 116 121 112 101 34 58 32 34 70 101 97 116 117 114 101 67
111 108 108 101 99 116 105 111 110 34 44 10 32 32 32 32 34 102 101 97
116 117 114 101 115 34 58 32 91 10 32 32 32 32 32 32 123 32 34 116 121
112 101 34 58 32 34 70 101 97 116 117 114 101 34 44 10 32 32 32 32 32
32 32 32 34 103 101 111 109 101 116 114 121 34 58 32 123 34 116 121
112 101 34 58 32 34 80 111 105 110 116 34 44 32 34 99 111 111 114 100
105 110 97 116 101 115 34 58 32 91 49 48 50 46 48 44 32 48 46 53 93
125 44 10 32 32 32 32 32 32 32 32 34 112 114 111 112 101 114 116 105
101 115 34 58 32 123 34 112 114 111 112 48 34 58 32 34 118 97 108 117
101 48 34 125 10 32 32 32 32 32 32 32 32 125 44 10 32 32 32 32 32 32
123 32 34 116 121 112 101 34 58 32 34 70 101 97 116 117 114 101 34 44
10 32 32 32 32 32 32 32 32 34 103 101 111 109 101 116 114 121 34 58 32
123 10 32 32 32 32 32 32 32 32 32 32 34 116 121 112 101 34 58 32 34 76
105 110 101 83 116 114 105 110 103 34 44 10 32 32 32 32 32 32 32 32 32
32 34 99 111 111 114 100 105 110 97 116 101 115 34 58 32 91 10 32 32
32 32 32 32 32 32 32 32 32 32 91 49 48 50 46 48 44 32 48 46 48 93 44
32 91 49 48 51 46 48 44 32 49 46 48 93 44 32 91 49 48 52 46 48 44 32
48 46 48 93 44 32 91 49 48 53 46 48 44 32 49 46 48 93 10 32 32 32 32
32 32 32 32 32 32 32 32 93 10 32 32 32 32 32 32 32 32 32 32 125 44 10
32 32 32 32 32 32 32 32 34 112 114 111 112 101 114 116 105 101 115 34
58 32 123 10 32 32 32 32 32 32 32 32 32 32 34 112 114 111 112 48 34 58
32 34 118 97 108 117 101 48 34 44 10 32 32 32 32 32 32 32 32 32 32 34
112 114 111 112 49 34 58 32 48 46 48 10 32 32 32 32 32 32 32 32 32 32
125 10 32 32 32 32 32 32 32 32 125 44 10 32 32 32 32 32 32 123 32 34
116 121 112 101 34 58 32 34 70 101 97 116 117 114 101 34 44 10 32 32
32 32 32 32 32 32 32 34 103 101 111 109 101 116 114 121 34 58 32 123
10 32 32 32 32 32 32 32 32 32 32 32 34 116 121 112 101 34 58 32 34 80
111 108 121 103 111 110 34 44 10 32 32 32 32 32 32 32 32 32 32 32 34
99 111 111 114 100 105 110 97 116 101 115 34 58 32 91 10 32 32 32 32
32 32 32 32 32 32 32 32 32 91 32 91 49 48 48 46 48 44 32 48 46 48 93
44 32 91 49 48 49 46 48 44 32 48 46 48 93 44 32 91 49 48 49 46 48 44
32 49 46 48 93 44 10 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 91
49 48 48 46 48 44 32 49 46 48 93 44 32 91 49 48 48 46 48 44 32 48 46
48 93 32 93 10 32 32 32 32 32 32 32 32 32 32 32 32 32 93 10 32 32 32
32 32 32 32 32 32 125 44 10 32 32 32 32 32 32 32 32 32 34 112 114 111
112 101 114 116 105 101 115 34 58 32 123 10 32 32 32 32 32 32 32 32 32
32 32 34 112 114 111 112 48 34 58 32 34 118 97 108 117 101 48 34 44 10
32 32 32 32 32 32 32 32 32 32 32 34 112 114 111 112 49 34 58 32 123 34
116 104 105 115 34 58 32 34 116 104 97 116 34 125 10 32 32 32 32 32 32
32 32 32 32 32 125 10 32 32 32 32 32 32 32 32 32 125 10 32 32 32 32 32
32 32 93 10 32 32 32 32 32 125]
After than I Unmarshal it and print again and I got this
{FeatureCollection [{Feature map[prop0:value0] {Point [102
0.5]}} {Feature map[prop0:value0 prop1:0] {LineString [[102 0] [103 1] [104 0] [105 1]]}} {Feature map[prop0:value0 prop1:map[this:that]]
{Polygon [[[100 0] [101 0] [101 1] [100 1] [100 0]]]}}]}
Than I Marshal it and got this
[123 34 116 121 112 101 34 58 34 70 101 97 116 117 114 101 67 111 108
108 101 99 116 105 111 110 34 44 34 102 101 97 116 117 114 101 115 34
58 91 123 34 116 121 112 101 34 58 34 70 101 97 116 117 114 101 34 44
34 112 114 111 112 101 114 116 105 101 115 34 58 123 34 112 114 111
112 48 34 58 34 118 97 108 117 101 48 34 125 44 34 103 101 111 109 101
116 114 121 34 58 123 34 116 121 112 101 34 58 34 80 111 105 110 116
34 44 34 99 111 111 114 100 105 110 97 116 101 115 34 58 91 49 48 50
44 48 46 53 93 125 125 44 123 34 116 121 112 101 34 58 34 70 101 97
116 117 114 101 34 44 34 112 114 111 112 101 114 116 105 101 115 34 58
123 34 112 114 111 112 48 34 58 34 118 97 108 117 101 48 34 44 34 112
114 111 112 49 34 58 48 125 44 34 103 101 111 109 101 116 114 121 34
58 123 34 116 121 112 101 34 58 34 76 105 110 101 83 116 114 105 110
103 34 44 34 99 111 111 114 100 105 110 97 116 101 115 34 58 91 91 49
48 50 44 48 93 44 91 49 48 51 44 49 93 44 91 49 48 52 44 48 93 44 91
49 48 53 44 49 93 93 125 125 44 123 34 116 121 112 101 34 58 34 70 101
97 116 117 114 101 34 44 34 112 114 111 112 101 114 116 105 101 115 34
58 123 34 112 114 111 112 48 34 58 34 118 97 108 117 101 48 34 44 34
112 114 111 112 49 34 58 123 34 116 104 105 115 34 58 34 116 104 97
116 34 125 125 44 34 103 101 111 109 101 116 114 121 34 58 123 34 116
121 112 101 34 58 34 80 111 108 121 103 111 110 34 44 34 99 111 111
114 100 105 110 97 116 101 115 34 58 91 91 91 49 48 48 44 48 93 44 91
49 48 49 44 48 93 44 91 49 48 49 44 49 93 44 91 49 48 48 44 49 93 44
91 49 48 48 44 48 93 93 93 125 125 93 125]
After Unmarshalling I got the same
{FeatureCollection [{Feature map[prop0:value0] {Point [102 0.5]}}
{Feature map[prop0:value0 prop1:0] {LineString [[102 0] [103 1] [104
0] [105 1]]}} {Feature map[prop0:value0 prop1:map[this:that]] {Polygon
[[[100 0] [101 0] [101 1] [100 1] [100 0]]]}}]}
Why is that so? Why do I have different number of bytes and why after Unmarshalling these different bytes I got the same result?
The original file contains many spaces and new-lines, you can see them in your output (32 = Space). json.Unmarshal() will remove all those spaces and json.Marshal() won't add them again. If you want to pretty format your second output you can use json.MarshalIndent().
I'm trying to use a variable range with ruby, but my code does not work;
ruby -e ' input2=145..170 ; input3= input2.to_s.gsub(/(.*?)\.\.(.*?)/) { 5.upto($2.to_i) { |i| print i, " " } }; print input3' > zzmf
But I obtained 5170
This part fails:
5.upto($2.to_i) { |i| print i, " " }
I expected:
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 5170
I don't think gsub is what you need, try the match example below. [2] gets the second match from the regex /(\d+)..(\d+)/ applied to "147..170"
5.upto("147..170".match(/(\d+)\.\.(\d+)/)[2].to_i) { |i| print i, " "}
gsub is intended for string substitution.
https://ruby-doc.org/core-2.1.4/String.html#method-i-gsub
I see my code and I confuse in regular expression
I use this .*?
and the correct is this .*
(.*)/
ruby -e ' input2=145..170 ; input3= input2.to_s.gsub(/(.*?)\.\.(.*)/) { 5.upto($2.to_i) { |i| print i, " " } }; print input3' > zzmf
thanks for your responses
I am using Bcrypt in Go to hash and compare the password given by the user. The thing is in the login, when I compare the password using CompareHashAndPassword it never matches so always says that the password is incorrect. Based on the concept of hash is supposed that with the same input we will have anytime the same output, and this is not my case.
**My code to hash (in the sign up) **
bs, err := bcrypt.GenerateFromPassword([]byte(Password), bcrypt.MinCost)
What I did
Send as password: 12345
When I print bs I get:
Attempt 1: [36 50 97 36 48 52 36 49 104 78 117 77 56 73 113 99 114 78 99 111 100 57 57 120 101 69 117 118 117 103 87 108 68 76 88 70 119 110 65 116 68 108 118
57 68 86 81 88 77 50 71 78 101 81 104 65 54 67 107 121]
Attempt 2:
[36 50 97 36 48 52 36 47 50 84 70 73 120 56 70 67 116 69 101 48 113 86 89 103 89 119 71 97 46 120 77 116 83 86 57 56 112 122 66 103 46 106 74 104 10
8 82 113 117 85 110 51 103 115 107 109 102 109 49 115 113]
Attempt 3:
[36 50 97 36 48 52 36 51 103 97 117 103 49 74 110 113 85 101 113 54 121 69 108 109 72 76 108 72 46 85 121 65 87 122 103 119 88 71 82 114 56 105 65 6
9 49 113 73 112 52 48 85 69 85 47 118 56 56 47 48 67]
Correct me if I am wrong, but in all that attempts the result should not be the same?
Then, I save that values in the database and these are the values for each attempt:
$2a$04$1hNuM8IqcrNcod99xeEuvugWlDLXFwnAtDlv9DVQXM2GNeQhA6Cky
$2a$04$/2TFIx8FCtEe0qVYgYwGa.xMtSV98pzBg.jJhlRquUn3gskmfm1sq
$2a$04$3gaug1JnqUeq6yElmHLlH.UyAWzgwXGRr8iAE1qIp40UEU/v88/0C
Then, to compare the password, in the login:
err := bcrypt.CompareHashAndPassword(user.Password, []byte(p))
user.Password is a []byte this value is conusulted from the database
Thank you
p is the password send in the form by the user
Bcrypt generates a random salt (that is included as a part of the resulting hash). So it is different every time with purpose.
You need to use bcrypt.CompareHashAndPassword to compare the hashed password and the plaintext password.
The first argument of bcrypt.CompareHashAndPassword is the hashed password, the second is the plaintext password. So you passed them in the wrong order.
WARNING: the cost you've chosen 4 is extremely low. Consider choosing something like 10 or over.
I am working on a API in Golang, am using Gorm as ORM. Currently am having an issue with a []byte field, I have it defined in my struct as:
type Member struct {
MyField []byte `gorm:"column:MyField" schema:"-"`
}
Then, I have the methods to Save and Read that resource, so the value when I save it is different to value that I am reading. For example,am using bcrypt to generate the hashed password, trying with 12345 the result is:
[36 50 97 36 49 48 36 46 56 98 88 72 82 71 113 66 100 65 105 103 70 119 114 97 73 77 99 78 117 106 54 78 103 88 68 49 56 110 103 112 105 86
104 79 117 47 114 57 116 51 47 53 97 100 109 103 106 46 68 109]
Until there everything is ok, then when I read the register from the database and print that value I get:
[36 50 97 36 48 52 36 56 49 67 66 121 118 90 47 47 104 49 83 120 50 108 112 71 73 51 67 88 46 97 52 74 54 66 84 73 106 105 110 122 98 69 90 51
78 113 67 66 49 103 50 56 116 47 57 120 78 103 109 54]
They are different, why?
I used gorm to create the tables, and the type of that column is defined in my postgres database as bytea
The code
To save:
bs, err := bcrypt.GenerateFromPassword([]byte(Pass), bcrypt.DefaultCost)
if err != nil {
fmt.Println("Hey error!")
}
user.MyField = bs
db.Create(&user)
To Read:
db.First(&user,id)
fmt.Println(user.MyField)
I suppose your problem is a wrong id at db.First(&user,id). You probably get the wrong row. I've tried to repoduce your error but couldn't. The following code works fine:
bs, err := bcrypt.GenerateFromPassword([]byte("12345"), bcrypt.DefaultCost)
expected := bs
db.Create(&Member{MyField: bs})
var member Member
db.First(&member)
actual := member.MyField
if !bytes.Equal(actual, expected) {
panic("fields are not equal")
}