Last active
July 7, 2023 09:33
-
-
Save tom-code/698b20b342be7bbf6ab692884b8476d5 to your computer and use it in GitHub Desktop.
golang h2c http2 client and server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"golang.org/x/net/http2" | |
"golang.org/x/net/http2/h2c" | |
"crypto/tls" | |
"net" | |
) | |
func client() { | |
fmt.Println("s1") | |
client := &http.Client{} | |
client.Transport = &http2.Transport{ | |
AllowHTTP: true, | |
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) { | |
return net.Dial(netw, addr) | |
}} | |
resp, err := client.Get("http://localhost:8080/anc") | |
fmt.Println(err) | |
fmt.Println(resp) | |
} | |
func main() { | |
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, "Hello world123") | |
}) | |
h2s := &http2.Server{ | |
// ... | |
} | |
h1s := &http.Server{ | |
Addr: ":8080", | |
Handler: h2c.NewHandler(handler, h2s), | |
} | |
go h1s.ListenAndServe() | |
client() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment