Create string from map[string]interface{} [closed] - string

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
data := map[string]interface{}{
"username": 'username1',
"password": 'password1'
}
I have a map[string]interface{} data, I want to form a string like below
str = "username='username1', password='password1'"
How can I form string like above i.e. key1='value1', key2='value2'....
I am new to golang
I tried append() but was facing issues like cannot append []string to variable type string

Have you tried anything? There's lots of ways to do what you're trying to do. Some more peprformant than others, some easier to write... This would be a quick way to implement what you need:
func PrintStr(m map[string]interface{}) {
parts := make([]string, 0, len(m))
for k, v := range m {
parts = append(parts, fmt.Sprintf("%s=%v", k, v))
}
fmt.Printf("%s\n", strings.Join(parts, ";"))
}

Range over data. Add comma if not first. Write key. Write =. Write value.
data := map[string]interface{}{
"username": "username1",
"password": "password1",
}
var sb strings.Builder
for k, v := range data {
if sb.Len() > 0 {
sb.WriteString(", ")
}
sb.WriteString(k)
sb.WriteString(`="`)
sb.WriteString(fmt.Sprint(v))
sb.WriteString(`"`)
}
s := sb.String()
fmt.Println(s)

Related

sort in Ascending Order [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Input->windows 7 was released on 1 Jan 2017
Output
1
7
2017
Jan
on
released
was
windows
Did little example to understand object parsing into int type parameter.
List<object> _obj = new List<object>() { "10", "20", "30", "d", "a", "t" };
int sum = 0;
for (int i = 0; i < _obj.Count; i++)
{
int temp;
//parsing object into int type, when we cant parse object will be 0
int.TryParse(_obj[i].ToString(), out temp);
//sum values
sum = sum + temp;
}

Extracting quoted part from string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying following code to extract quoted part from a string:
package main
import ("fmt")
func main(){
var oristr = "This is a \"test string\" for testing only"
var quotedstr = ""
var newstr = ""
var instring = false
fmt.Println(oristr)
for i,c := range oristr {
fmt.Printf("Char number: %d; char: %c\n", i, c);
if c = `"` {
if instring
{instring=false}
else {instring=true}}
if instring
{quotedstr += c}
else {newstr += c}
}
fmt.Printf("Newstr: %s; quotedstr = %s", newstr, quotedstr )
}
However, I am getting following error:
# command-line-arguments
./getstring.go:11:14: syntax error: c = `"` used as value
./getstring.go:12:15: syntax error: unexpected newline, expecting { after if clause
./getstring.go:14:4: syntax error: unexpected else, expecting }
./getstring.go:15:3: syntax error: non-declaration statement outside function body
Why I am getting this error and how can this be corrected?
Also, is this approach all right or some other approach may be better?
This is the most basic way of getting what you want. It could be improved to be more robust etc.
package main
import (
"fmt"
"regexp"
)
func main() {
var oristr = "This is a \"test string\" for containing multiple \"test strings\" and another \"one\" here"
re := regexp.MustCompile(`"[^"]+"`)
newStrs := re.FindAllString(oristr, -1)
for _, s := range newStrs {
fmt.Println(s)
}
}

Type Assertion Non Parsimonious [duplicate]

This question already has an answer here:
Avoid using type assertions in the branches of a type switch
(1 answer)
Closed 3 years ago.
I have the following, which works:
reflectItem := reflect.ValueOf(dataStruct)
subItem := reflectItem.FieldByName(subItemKey)
switch subItem.Interface().(type) {
case string:
subItemVal := subItem.Interface().(string)
searchData = bson.D{{"data." +
strings.ToLower(subItemKey), subItemVal}}
case int64:
subItemVal := subItem.Interface().(int64)
searchData = bson.D{{"data." +
strings.ToLower(subItemKey), subItemVal}}
}
The issue is that this seems very non-parsimonious. I would very much like to simply get the type of subItem without having a switch statement that simply asserts back its own type after finding the field by name. I'm not sure how to back this out however. Ideas?
I don't understand your question exactly, but what you're doing can be easily shortened without affecting functionality:
reflectItem := reflect.ValueOf(dataStruct)
subItem := reflectItem.FieldByName(subItemKey)
switch subItemVal := subItem.(type) {
case string:
searchData = bson.D{{"data." +
strings.ToLower(subItemKey), subItemVal}}
case int64:
searchData = bson.D{{"data." +
strings.ToLower(subItemKey), subItemVal}}
}
But beyond that, I don't think a type assertion is necessary in your case at all. This should also work:
reflectItem := reflect.ValueOf(dataStruct)
subItem := reflectItem.FieldByName(subItemKey)
searchData = bson.D{{"data."+strings.ToLower(subItemKey), subItem.Interface())

How to randomize the case of letters in a string in Swift? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Given a string, I would like to change the case of each letter to a random between upper and lower case.
For example:
let myString = "Have a nice day!"
let newString = "haVe A NiCe dAY!"
Swift 2
I would use arc4random_uniform and map.
let myString = "Have a nice day!"
let result = myString.characters.map { (char) -> String in
if arc4random_uniform(2) == 0 {
return String(char).lowercaseString
}
return String(char).uppercaseString
}.joinWithSeparator("")
print(result)
hAvE A NICe DAy!
Swift 3:
let result = myString.characters.map {
if arc4random_uniform(2) == 0 {
return String($0).lowercased()
}
return String($0).uppercased()
}.joined(separator: "")
Swift 4.2
let result = myString.map {
if Int.random(in: 0...1) == 0 {
return String($0).lowercased()
}
return String($0).uppercased()
}.joined(separator: "")

Generate auto increment number in a textbox using C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to add sequence numbers in textbox but the format is 001 to onward, the main problem is when the number comes to 009, after it became 0010, four digit character. I want to reduce a zero from it, the number should look like 010. Please help me for this problem
This should meet your needs.
for (int i = 0; i <= 100; ++i)
{
Console.WriteLine(string.Format("{0:000}", i));
}
Quick and dirty. Probably not the best way to do this:
textBox1.Text = (Convert.ToInt16(textBox1.Text) + 1).ToString();
if (textBox1.Text.Length == 1)
{
textBox1.Text = "00" + textBox1.Text;
}
else if (textBox1.Text.Length == 2)
{
textBox1.Text = "0" + textBox1.Text;
}

Resources