Go Programming Pitfalls and Networking Solutions

Private IP Address Ranges

RFC 1918 defines three IP address blocks reserved for private networks:

  • Class A: 10.0.0.0 – 10.255.255.255
  • Class B: 172.16.0.0 – 172.31.255.255
  • Class C: 192.168.0.0 – 192.168.255.255

These addresses are not routable on the public internet and can be used internally without ISP registration.

Docker Networking Issues

After installing Docker, you might encounter connectivity problems. Docker automatically adds itself to system startup and may disrupt network connections due to proxy settings.

The error message "Remote computer or device does not accept connection" can be resolved by:

  1. Open Internet Options
  2. Navigate to Connections tab
  3. Click LAN Settings
  4. Uncheck all proxy server options

Docker Proxy Configuration

To configure Docker proxy settings:

  1. Open Docker client
  2. Click the settings gear icon
  3. Navigate to Resource > Proxies
  4. Enable Manual proxy configuration
  5. Set HTTP proxy (e.g., socks5://127.0.0.1:10808/)

Proxy configuration is necessary when accessing restricted resources like gcr.io.

Go Modules Error Resolution

When encountering "cannot find main module; see 'go help modules'" in VSCode:

This error occurs when GO111MODULE is set to 'on' but no go.mod file exists in the project directory.

Solutions:

  1. Create a go.mod file
  2. Set GO111MODULE to 'auto' via environment variables
  3. Restart the system

VSCode Go Debug Configuration

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Go Debugger",
            "type": "go",
            "request": "launch",
            "program": "${fileDirname}",
            "env": {
                "GOPATH": "c:/gowork/mygo",
                "GOROOT": "c:/GO"
            },
            "args": [
                "-config",
                "config.json"
            ]
        }
    ]
}

Go Language Fundamentals

  • fmt package provides formatting functions similar to C++ iostream
  • Every Go application contains a main package
  • func main() is the entry point for execution
  • Comments use // for single-line and /* ... */ for blocks
  • Exported identifiers start with uppercase letters (public), unexported with lowercase (private)
  • Go's automatic semicolon insertion rules eliminate debates about brace placement
  • Variables declared with := inside functions, var for package-level
  • Multiple return values: func swap(a, b int) (int, int)
  • Structs use type keyword: type Book struct { Title string }
  • JSON serialization requires exported fields (uppercase)
  • Slices are dynamic arrays with len() and cap() functions
  • range keyword iterates over collections, maps, and strings
  • Goroutines enable concurrency with the go keyword
  • Channels (chan) facilitate communication between goroutines
  • defer executes functions when their containing functon returns

Handling JSON with Socket.IO in Go

Using the gosocketio library:

c.On("success", func(f interface{}) {
    m := f.(map[string]interface{})
    for k, v := range m {
        switch vv := v.(type) {
        case string:
            fmt.Println(k, "is string", vv)
        case int:
            fmt.Println(k, "is int", vv)
        case []interface{}:
            fmt.Println(k, "is an array:")
            for i, u := range vv {
                fmt.Println(i, u)
            }
        default:
            fmt.Println(k, "is of unknown type")
        }
    }
})

Merging Map Slices in Go

When working with slices of maps:

var combinedData []map[string]interface{}
slice1 := []map[string]interface{}{{"one": 1, "two": 2}}
slice2 := []map[string]interface{}{{"three": 3, "four": 4}}

combinedData = append(combinedData, slice1...)
combinedData = append(combinedData, slice2...)

// Accessing data
for _, item := range combinedData {
    for k, v := range item {
        fmt.Printf("%s: %v\n", k, v)
    }
}

Command Line Arguments and File Operations

var configFile string

func init() {
    flag.StringVar(&configFile, "config", "config.json", "Path to configuration file")
    flag.Parse()
}

func main() {
    configData, err := ioutil.ReadFile(configFile)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(configData))
}

Tags: Go docker networking JSON Socket.io

Posted on Mon, 14 Sep 2026 16:35:29 +0000 by Mikedean