Hello app source code

Source code for GKE Hello app server.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Go

To authenticate to GKE, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  package 
  
 main 
 import 
  
 ( 
  
 "fmt" 
  
 "log" 
  
 "net/http" 
  
 "os" 
 ) 
 func 
  
 main 
 () 
  
 { 
  
 // register hello function to handle all requests 
  
 mux 
  
 := 
  
 http 
 . 
 NewServeMux 
 () 
  
 mux 
 . 
 HandleFunc 
 ( 
 "/" 
 , 
  
 hello 
 ) 
  
 // use PORT environment variable, or default to 8080 
  
 port 
  
 := 
  
 os 
 . 
 Getenv 
 ( 
 "PORT" 
 ) 
  
 if 
  
 port 
  
 == 
  
 "" 
  
 { 
  
 port 
  
 = 
  
 "8080" 
  
 } 
  
 // start the web server on port and accept requests 
  
 log 
 . 
 Printf 
 ( 
 "Server listening on port %s" 
 , 
  
 port 
 ) 
  
 log 
 . 
 Fatal 
 ( 
 http 
 . 
 ListenAndServe 
 ( 
 ":" 
 + 
 port 
 , 
  
 mux 
 )) 
 } 
 // hello responds to the request with a plain-text "Hello, world" message. 
 func 
  
 hello 
 ( 
 w 
  
 http 
 . 
 ResponseWriter 
 , 
  
 r 
  
 * 
 http 
 . 
 Request 
 ) 
  
 { 
  
 log 
 . 
 Printf 
 ( 
 "Serving request: %s" 
 , 
  
 r 
 . 
 URL 
 . 
 Path 
 ) 
  
 host 
 , 
  
 _ 
  
 := 
  
 os 
 . 
 Hostname 
 () 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Hello, world!\n" 
 ) 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Version: 1.0.0\n" 
 ) 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Hostname: %s\n" 
 , 
  
 host 
 ) 
 } 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Design a Mobile Site
View Site in Mobile | Classic
Share by: