A simple GET request in GoLang
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"net" | |
) | |
func main() { | |
ExampleGETReq() | |
AlternateExampleGETReq() | |
} | |
func ExampleGETReq() { | |
addr, err := net.ResolveTCPAddr("tcp", "google.com:80") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Open a connection to google.com | |
conn, err := net.DialTCP("tcp", nil, addr) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Write to the connection | |
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n") | |
// Listen for reply and process it | |
status, err := bufio.NewReader(conn).ReadString('\n') | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("Status: ", status) | |
// Finally close the connection | |
conn.Close() | |
} | |
func AlternateExampleGETReq() { | |
// Open a connection to google.com | |
conn, err := net.Dial("tcp", "google.com:80") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Write to the connection | |
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n") | |
// Listen for reply and process it | |
status, err := bufio.NewReader(conn).ReadString('\n') | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("Status: ", status) | |
// Finally close the connection | |
conn.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment