Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6472171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:18:43+00:00 2026-05-25T06:18:43+00:00

I’m having some trouble with strings in Golang. It seems that they don’t get

  • 0

I’m having some trouble with strings in Golang. It seems that they don’t get handed over to another function.

func Sendtext(ip string, port string, text string) (err int) {
targ := ip + ":" + port
raddr,e := net.ResolveTCPAddr("tcp",targ)
if e != nil {
    os.Stdout.WriteString(e.String()+"\n")
    return 1
}
conn,e := net.DialTCP("tcp",nil,raddr)
if e != nil {
    os.Stdout.WriteString(e.String()+"\n")
    return 1
}
conn.Write([]byte(text))
mess := make([]byte,1024)
conn.Read(mess)
message := string(mess)
conn.Close()
if message[0] == 'a' {
    return 0
} else {
    return 1
}
return 0
}

func main() {
os.Stdout.WriteString("Will send URL: ")
url := GetURL()
os.Stdout.WriteString(url + "\n\n")
_, port, pass, ip := browserbridge_config.ReadPropertiesFile()
os.Stdout.WriteString("sending this url to " + ip + ":" + port + "\n")
message := url + "\n" + pass + "\n"
os.Stdout.WriteString("\nsending... ")
e := Sendtext(ip, port, message)
if e != 0 {
    os.Stdout.WriteString("ERROR\n")
    os.Exit(e);
}
os.Stdout.WriteString("DONE\n")
}

and my config reader:

func ReadConfigFile(filename string) (browsercommand string, port string, pass string, ip string) {

// set defaults
browsercommand = "%u"
port = "7896"
pass = "hallo"
ip = "127.0.0.1"

// open file
file, err := os.Open(filename)
if err != nil {
    os.Stdout.WriteString("Error opening config file. proceeding with standard config...")
    return
}


// Get reader and buffer
reader := bufio.NewReader(file)

for {
    part,_,err := reader.ReadLine()
    if err != nil {
        break
    }
    buffer := bytes.NewBuffer(make([]byte,2048))
    buffer.Write(part)
    s := strings.ToLower(buffer.String())

    if strings.Contains(s,"browsercommand=") {
        browsercommand = strings.Replace(s,"browsercommand=","",1)
    } else {
        if strings.Contains(s,"port=") {
            port = strings.Replace(s,"port=","",1)
        } else {
            if strings.Contains(s,"password=") {
                pass = strings.Replace(s,"password=","",1)
            } else {
                if strings.Contains(s,"ip=") {
                    ip = strings.Replace(s,"ip=","",1)
                }
            }
        }
    }
}

return
}

Output of this program:

Will send URL: test.de

sending this url to 192.168.2.100:7896

sending... 
dial tcp 192.168.2.1:0: connection refused
ERROR

(192.168.2.1 is gateway)

I tried to os.Stdout.WriteString(targ) or os.Stdout.WriteString(ip) just at the top of Sendtext, and got no output.

The confusing thing about it: yesterday it worked xD (before I migrated ReadConfig into its own .go file)

I hope you can help me solving this…

sylar


Update:

As PeterSO said, the problem is not the handover of the strings
My first guess, that it must be the conversion of String to TCPAddr, is true, but it seems to be a problem with the strings, not with the net library.
I just added
ip = “192.168.2.100”
port = “7896”
right after the call of Sendtext, and that helped… (at least until a user needs to set a custom ip/port…)

I know that the problem firs occured when I decided to switch from goconf (http://code.google.com/p/goconf/) to my own. This is why I think the problem is in the ReadProperties() function.

I also realized that strconv.Atoi(port) returns 0 (parsing “7896”: invalid argument)
When I used the server and client with implemented (not-changable) config, and then let the client read the password from the config file, the password comparison fails. When I also set the password right in the code (without reading a file), it works.

I really don’t know what to do now… Any idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:18:44+00:00Added an answer on May 25, 2026 at 6:18 am

    Go bytes package: func NewBuffer(buf []byte) *Buffer

    NewBuffer creates and initializes a new Buffer using buf as its
    initial contents. It is intended to prepare a Buffer to read
    existing data. It can also be used to size the internal buffer for
    writing. To do that, buf should have the desired capacity but a
    length of zero.

    In most cases, new(Buffer) (or just declaring a Buffer variable)
    is preferable to NewBuffer. In particular, passing a non-empty buf
    to NewBuffer and then writing to the Buffer will overwrite buf,
    not append to it.

    In your ReadConfigFile function, you write:

    buffer := bytes.NewBuffer(make([]byte,2048))
    buffer.Write(part)
    

    The make([]byte,2048) function call creates an initial slice for buffer with a length and capacity of 2048 bytes. The buffer.Write(part) function call writes part by overwriting buffer. At the very least, you should have written make([]byte,0,2048) to initially give the buffer slice a length of zero and capacity of 2048 bytes.

    Your ReadConfigFile function has other flaws. For example, the key=value format is very rigid, only keys hardcoded into the function are recognized, if a configuration file is not given it doesn’t return the defaults, the configuration file is not closed, etc. Here’s a basic implementation of a configuration file reader.

    package main
    
    import (
        "bufio"
        "fmt"
        "os"
        "strings"
    )
    
    type Config map[string]string
    
    func ReadConfig(filename string) (Config, os.Error) {
        config := Config{
            "browsercommand": "%u",
            "port":           "7896",
            "password":       "hallo",
            "ip":             "127.0.0.1",
        }
        if len(filename) == 0 {
            return config, nil
        }
        file, err := os.Open(filename)
        if err != nil {
            return nil, err
        }
        defer file.Close()
        rdr := bufio.NewReader(file)
        for {
            line, err := rdr.ReadString('\n')
            if eq := strings.Index(line, "="); eq >= 0 {
                if key := strings.TrimSpace(line[:eq]); len(key) > 0 {
                    value := ""
                    if len(line) > eq {
                        value = strings.TrimSpace(line[eq+1:])
                    }
                    config[key] = value
                }
            }
            if err == os.EOF {
                break
            }
            if err != nil {
                return nil, err
            }
        }
        return config, nil
    }
    
    func main() {
        config, err := ReadConfig(`netconfig.txt`)
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println("config:", config)
        ip := config["ip"]
        pass := config["password"]
        port := config["port"]
        fmt.Println("values:", ip, port, pass)
    }
    

    Input:

    [a section]
    key=value
    ; a comment
    port = 80
      password  =  hello  
     ip= 217.110.104.156
    # another comment
     url =test.de
    file =
    

    Output:

    config: map[browsercommand:%u key:value port:80 ip:217.110.104.156 url:test.de
    file: password:hello]
    values: 217.110.104.156 80 hello
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.