Q:

golang http server send r.URL.Path over socket

I have a http server, and I want to send the r.URL.Path text to a client using a socket
I get a error:  undefined: conn in conn.Write
This is becauase conn is defined in another function
What I have tried:
package main

import (
    "net"
    "io"
    "net/http"
)

ln, _ := net.Listen("tcp", ":8081")
conn, _ := ln.Accept()

func hello(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "Hello world!")
    conn.Write([]byte(r.URL.Path + "\n")) //Here I'm attemping to send it
}

func main() {

    http.HandleFunc("/", hello)
    http.ListenAndServe(":8000", nil)
}

A:

Your problem is actually in the way you try to declare variables.
If you want your conn to be on global scope, use var
package main

import (
    "io"
    "net/http"
    "net"
)

var ln, _ = net.Listen("tcp", ":8081")
var conn, _ = ln.Accept()

func hello(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "Hello world!")
    conn.Write([]byte(r.URL.Path + "\n")) //Here I'm attemping to send it
}

func main() {
    http.HandleFunc("/", hello)
    http.ListenAndServe(":8000", nil)
}

