Last active
February 22, 2016 18:25
-
-
Save gouthamve/7c507e49c083d3438ed1 to your computer and use it in GitHub Desktop.
A simple GET request in GoLang
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 ( | |
"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