Basic go syntax
Declare a variable and assign a value
// define a new variable and assign a value
var name = "John"
// define a variable to but not assign a value
var id int
// or
// define a struct to hold the JSON data
type Person struct {
Name string
Age int
}
// define a variable to hold the JSON data
var p Person
// decode the JSON data into the variable
err := json.NewDecoder(r.Body).Decode(&p)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// define a struct to hold the JSON data
type Person struct {
Name string
Age int
}
// define a variable to hold the JSON data
p := Person{
Name: "John",
Age: 42,
}
// encode the JSON data into the variable
err := json.NewEncoder(w).Encode(p)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// generate a random number between 0 and 100
var nr int
nr = rand.Intn(100)
// or
nr := rand.Intn(100)
// convert a int to a string
var nr int
nr = 42
var str string
str = strconv.Itoa(nr)
// define a slice
var slice []string
// add a new item to the slice
slice = append(slice, "item")
// works for all types
var usrs []User
usrs = append(usrs, User{Name: "John", Age: 42})
// create a loop
for i := 0; i < 10; i++ {
fmt.Println(i)
}
// or if you want to loop over a slice
// where i is the index and item is the value
for i, item := range slice {
fmt.Println(i, item)
}
// define a slice
var slice []string
// add a new item to the slice
slice = append(slice, "item")
// remove an item from the slice
slice = append(slice[:index], slice[index+1:]...)
// works for all types
var users []User{{Name: "John", Age: 42}, {Name: "Jane", Age: 42}}
// remove the user at index 1
users = append(users[:1], users[1+1:]...)
func handler(w http.ResponseWriter, r *http.Request) {
// set the content type
w.Header().Set("Content-Type", "application/json")
// write the message
w.Write([]byte(`{"message": "Hello World"}`))
// or
fmt.Fprintf(w, `{"message": "Hello World"}`)
// if you need to embed a variable in the message
fmt.Fprintf(w, `{"message": "Hello %s"}`, name)
// or
fmt.Fprintf(w, `{"message": "Hello %s", "name": %s}`, name, name)
// embeded variable types you might need
// %s - string
// %d - int
}
// define a variable to hold the JSON data
var id int
// decode the JSON data into the variable
err := json.NewDecoder(r.Body).Decode(&id)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
func handler(w http.ResponseWriter, r *http.Request) {
// get the query parameter
name := r.URL.Query().Get("name")
// or
name := r.FormValue("name")
}
// Dial returns the connection, a http.Request and an error,
// we can ignore the http.Request for now.
conn, _, err := websocket.DefaultDialer.Dial("ws://localhost:8080/chat", nil)
if err != nil {
log.Fatal("dial:", err)
}
// read line by line from the console
scanner := bufio.NewScanner(os.Stdin)
// buffer input until a new line is found
for scanner.Scan() {
// get the text from the scanner
text := scanner.Text()
// do something with the text
}
// write a message to the websocket connection
err := conn.WriteMessage(websocket.TextMessage, []byte("Hello World"))
if err != nil {
log.Println("write:", err)
return
}
// t: type of message
// msg: message (in []byte format)
// error: error if any
t, msg, err := conn.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
text := string(msg)
// print the message to stdout (standard output)
fmt.Fprintf(os.Stdout, "%s\n", text)
// switch statements
switch t {
case websocket.TextMessage:
// do something
case websocket.BinaryMessage:
// do something
default:
// do something
// as we do not expect or handle this, this is a good place to return an error to our error channel
}