I've found some sort of similar problems in c, but the solutions are c specific. package main
Here is a minimum working example of the code
import "fmt"
func main() {
var mode string
var base int
for {
fmt.Printf("(Base) [-->]: ")
fmt.Scanf("%d", &base)
fmt.Printf("(Mode) [-->]: ")
fmt.Scanf("%s", &mode)
}
}
My issue is that after asking for the mode input, it doesn't wait for input, and immediately skips to the beginning of the loop. Something like this:
(Base) [-->]: 5
(Mode) [-->]: (Base) [-->]:
I had the same problem, changefmt.Scanf("%d", &base) to fmt.Scanf("%d \n", &base). I think it's connected to output of Scanf() where extra newline not being consumed by first scanf. Above code may still work in some device without any errors.
import "fmt"
func main() {
var mode string
var base int
for {
fmt.Printf("(Base) [-->]: ")
fmt.Scanf("%d \n", &base)
fmt.Printf("(Mode) [-->]: ")
fmt.Scanf("%s \n", &mode)
}
}
Related
I'm new here in Golang world for a week.. I've been research and still don't know how to print from user input to physical printer, from USB to printer.
For example I have done with code, just need add other function to print..
package main
import "fmt"
func main() {
fmt.Println("First name: ")
var name1 string
fmt.Scanln(&name1)
)
fmt.Println("Last name: ")
var name2 string
fmt.Scanln(&name2)
fmt.Print("My full name is: ")
fmt.Print(name1 + " " + name2)
}
func print(){
//need function to print directly from USB to pyhsical printer..
//print from func main() as text, any format
}
I have no idea to create print func in golang from user input..
If successful, then after func main() println, then immediately send the command to print to my printer.. just using terminal, I don't need the GUI.
Thank you for helping me:')
I am trying to add a bunch of values in a map data type and after that trying to print it out. But it is performing strangely. When I am directly calling the map with the key it is giving me the correct output but not giving me any output when I am storing the key in a variable and then calling it. I am not been able to figure it out what is happening and why am I getting this kind of output. Can Somebody help me with the same.
package main
import (
"bufio"
"fmt"
"os"
)
func main(){
type Authentication struct {
password string
}
var authentication = map[string]Authentication{}
var user1 Authentication
user1.password = "abc"
authentication["def"] = user1
reader := bufio.NewReader(os.Stdin)
usid := readString(reader)
fmt.Println(authentication)
fmt.Println(authentication[usid])
fmt.Println(authentication["def"])
}
// Reading input functions
func readString(reader *bufio.Reader) string {
s, _ := reader.ReadString('\n')
for i := 0; i < len(s); i++ {
if s[i] == '\n' {
return s[:i]
}
}
return s
}
Input:
def
Output:
map[def:{abc}]
{abc}
You're trying to do the same thing twice in readString. But all you have to do is to cut it by one byte.
func readString(reader *bufio.Reader) string {
s, _ := reader.ReadString('\n')
return s[:len(s)-1]
}
The program in the question does not work when \r\n is used as the line terminator in stdin. The program removes the trailing \n from the line, but not the \r.
Fix by using bufio.Scanner instead of bufio.Reader to read lines from the input. The bufio.Scanner type removes line terminators.
func main() {
type Authentication struct {
password string
}
var authentication = map[string]Authentication{}
var user1 Authentication
user1.password = "abc"
authentication["def"] = user1
scanner := bufio.NewScanner(os.Stdin)
if !scanner.Scan() {
log.Fatal(scanner.Err())
}
usid := scanner.Text()
fmt.Println(authentication)
fmt.Println(authentication[usid])
fmt.Println(authentication["def"])
}
There can always be a better way of reading string, but I see your code works too. I ran it in my local and it gives the expected output:
From your description, I presume you are using go playground or any such platform. If that is so, the thing is, go playground doesn't take standard input, and your code has reader on os.Stdin. When I copy your code to playground and add the following line to check,
fmt.Printf("Length of usid: %d\nusid: %q\n", len(usid), usid)
I see the following output:
Length of usid: 0
usid: ""
Conclusion: There is no issue with variables, map or code, but just the stdin.
I have this:
if t.FieldName != "" {
if t.FieldName != item.FieldName {
panic(errors.New("FieldName does not match, see: ", t.FieldName, item.FieldName))
}
}
that won't compile because errors.New takes one string arg. So I need to do something like:
panic(errors.New(joinArgs("FieldName does not match, see: ", t.FieldName, item.FieldName)))
How can I implement joinArgs, so that it concatenates all it's strings arguments into one string?
The XY problem is asking about your attempted solution rather than your actual problem: The XY Problem. Your real problem is formatting panic error messages.
This is the normal solution to your real problem:
package main
import "fmt"
func main() {
t := struct{ FieldName string }{FieldName: "a t.FieldName"}
item := struct{ FieldName string }{FieldName: "an item.FieldName"}
panic(fmt.Sprintf("FieldName does not match, see: %v %v", t.FieldName, item.FieldName))
}
Playground: https://play.golang.org/p/DaOlcqUgV_H
Output:
panic: FieldName does not match, see: a t.FieldName an item.FieldName
This seemed to work, not sure if it's optimal tho
func joinArgs(strangs ...string) string {
buffer := bytes.NewBufferString("")
for _, s := range strangs {
buffer.WriteString(s)
}
return buffer.String()
}
Consider the following gist linked here:
Code:
package main
import (
"fmt"
)
type StateTransition struct {
msg Message
}
type Message interface {
To() *string
}
type Transaction struct {
data txdata
}
type txdata struct {
Recipient *string
}
func (t Transaction) To() (*string) {
return t.data.Recipient
}
func UnMask(n **string, k string) {
*n = &k
}
func main() {
toField := "Bob"
toPtr := &toField
txd := txdata{toPtr}
tx := Transaction{txd}
st := StateTransition{tx}
n1 := st.msg.To()
fmt.Printf("Hello, %s \n", *n1)
UnMask(&n1, "Joe")
fmt.Printf("Hello, %s \n", *n1)
n2 := st.msg.To()
fmt.Printf("Hello, %s \n", *n2)
}
Output
Hello, Bob
Hello, Joe
Hello, Bob
Expected Output
Hello, Bob
Hello, Joe
Hello, Joe
The result is the sequence "Bob, Joe, Bob" is printed whereas my intuition says that it should be "Bob, Joe, Joe" (this is also what i want it to print). Can someone experienced in go please explain to me enough about combining pointers, structs, and interfaces as they relate to this problem to give me some understanding about why I'm wrong, and how to fix it?
Unmask takes a pointer to a pointer, let's say pointer X to pointer Y, pointer Y points to the string value. Unmask then changes the pointer to which X is pointing to, Y is unchanged and points still to the same old string.
You can do this:
func UnMask(n **string, k string) {
**n = k
}
or
func UnMask(n *string, k string) {
*n = k
}
// ....
UnMask(n1, "Joe") // drop the '&'
I'm trying to make use of the flag package. My whole issue is that I need to specify groups/multiple values for the same parameter.
For example I need to parse a command as below:
go run mycli.go -action first -point 10 -action
second -point 2 -action 3rd -point something
I need to retrieve each group of action/point param. Is it possible?
package main
import (
"flag"
"fmt"
"strconv"
)
// Define a type named "intslice" as a slice of ints
type intslice []int
// Now, for our new type, implement the two methods of
// the flag.Value interface...
// The first method is String() string
func (i *intslice) String() string {
return fmt.Sprintf("%d", *i)
}
// The second method is Set(value string) error
func (i *intslice) Set(value string) error {
fmt.Printf("%s\n", value)
tmp, err := strconv.Atoi(value)
if err != nil {
*i = append(*i, -1)
} else {
*i = append(*i, tmp)
}
return nil
}
var myints intslice
func main() {
flag.Var(&myints, "i", "List of integers")
flag.Parse()
}
Ref: http://lawlessguy.wordpress.com/2013/07/23/filling-a-slice-using-command-line-flags-in-go-golang/
The flag package won't help you. Closest you'll get is the os package:
[jadekler#Jeans-MacBook-Pro:~/go/src]$ go run temp.go asdasd lkjasd -boom bam -hello world -boom kablam
[/var/folders/15/r6j3mdp97p5247bkkj94p4v00000gn/T/go-build548488797/command-line-arguments/_obj/exe/temp asdasd lkjasd -boom bam -hello world -boom kablam]
So, the first runtime flag key would be os.Args[1], the value would be os.Args[2], the next key would be os.Args[3], and so on.