👊
Go tutorial for Pythoner
  • Introduction
  • Install and Run Go
  • The structure of Go
    • First time you meet him
    • Functions
    • Return multiple results
    • Logic Control
    • Break loop and enumerate
    • List (Array or Slice)
    • Dict (Map)
    • Struct
    • Class (Methods)
    • Packages
  • Features of Go
  • Go modules
  • Web Server
    • Serving static files
    • A simple webserver
  • Go for Android
  • Where to go next
Powered by GitBook
On this page

Was this helpful?

  1. The structure of Go

List (Array or Slice)

package main

import (
	"fmt"
	"strings"
)

func main() {
	sentence := "I love you guys!"

	words := strings.Split(sentence, " ")
	fmt.Printf("%#v\n", words)

	for index, word := range words {
		fmt.Printf("%d. %s\n", index, word)
	}

	for _, word := range words {
		fmt.Printf("%s ", word)
	}
	
	new_words := [...]string{"Do", "you", "love", "me?"}
	for _, word := range new_words[:] {
			fmt.Printf("%s ", word)
	}
}
package main

import (
	"fmt"
)

//var nums = make([]int, 0) //this is a slice, it's a dynamic array
//you want to say it in this way: make a list of integer that has 0 element at the beginning
var nums []int

func main() {
	fmt.Printf("%#v\n", nums)

	nums = append(nums, 1)
	nums = append(nums, 9)
	nums = append(nums, 9)
	nums = append(nums, 8)

	fmt.Printf("%#v\n", nums)

	nums[len(nums)-1] = 0
	nums = nums[:len(nums)-1]

	fmt.Printf("%#v\n", nums)
}
PreviousBreak loop and enumerateNextDict (Map)

Last updated 3 years ago

Was this helpful?