Hello Go

本文介绍了Go语言中的包管理,特别是`main`包在可执行文件生成中的作用。还讨论了`import`语句用于引入其他包的功能,以及`funcmain()`作为程序入口点的角色。此外,文章涵盖了变量的声明和初始化,包括有效的和无效的示例,并探讨了函数的返回值类型。最后,提到了切片(slices)的使用和for循环遍历它们的基本方法。
摘要由CSDN通过智能技术生成

 Install

 Cards

How do we run the code in our project?

Use "go help <topic>" for more information about that topic.

PS E:\software\golang\goprojects\goproject1\HELLOWORLD> go run main.go
Hello World!
PS E:\software\golang\goprojects\goproject1\HELLOWORLD> go build main.go
PS E:\software\golang\goprojects\goproject1\HELLOWORLD> ls


    Directory: E:\software\golang\goprojects\goproject1\HELLOWORLD


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         1/10/2023   9:42 AM        1952256 main.exe
-a----         1/10/2023   9:19 AM             80 main.go


PS E:\software\golang\goprojects\goproject1\HELLOWORLD> ./main 
Hello World!
PS E:\software\golang\goprojects\goproject1\HELLOWORLD>

 

 What does 'package main' mean?

 

 

 

What does 'import "fmt" ' mean?

What's that 'func' thing?

How is the main.go file organized?

 Question 1:

What is the purpose of a package in Go?

 Question 2:

What is the special name we use for a package to tell Go that we want it to be turned into a file that can be executed?

Question 3: 

 The one requirement of packages named "main" is that we...

Question 4:

Why do we use "import" statements?

  • To give our package access to code written in another package

  • To optimize garbage collection

  • To declare functions we that our package should optimize

     Deeper into Go

package main

import "fmt"

func main() {

	var card string = "Ace of Spades"
	fmt.Println(card)
}

 

 

 

 

 Test Your Knowledge: Variable Assignment

Question 1:

Is the following a valid way of initializing and assigning a value to a variable?

var bookTitle string = "Harry Potter" 

  • Yes

  • No

  • Only sometimes

 

 Question 2:

Is the following a valid way of initializing an assigning a value to a variable?

fruitCount := 5 

Question 3:

After running the following code, Go will assume that the variable quizQuestionCount is of what type?

quizQuestionCount := 10 

 Question 4:

Will the following code compile?  Why or why not?

 
  1. paperColor := "Green"
  2. paperColor := "Blue"

 

Question 5:

Are the two lines following ways of initializing the variable 'pi' equivalent?

pi := 3.14 

var pi float64 = 3.14 

  • Yes

  • No

     

     Question 6:

    This might require a bit of experimentation on your end :).  Remember that you can use the Go Playground at https://play.golang.org/ to quickly run a snippet of code.

    Is the following code valid?

    package main
    
    import "fmt"
    
    deckSize := 20
    
    func main() {
    fmt.Println(deckSize)
    }

 Question 7:

We might be able to initialize a variable and then later assign a variable to it.  Is the following code valid?

package main
 
import "fmt"
 
func main() {
  var deckSize int
  deckSize = 52
  fmt.Println(deckSize)
}

 

Question 8:

Here's another one that might need some testing on your end! Remember that you can use the Go Playground at https://play.golang.org/ to quickly run a snippet of code.

Is the following code valid?

package main
 
import "fmt"
 
var deckSize int
 
func main() {
  deckSize = 50
  fmt.Println(deckSize)
}

 

 Question 9:

Is the following code valid?  Why or why not?

package main
 
import "fmt"
 
func main() {
  deckSize = 52
  fmt.Println(deckSize)
}

 

package main

import "fmt"

func main() {

	// var card string = "Ace of Spades"
	// card := "Ace of Spades"
	card := newCard()

	fmt.Println(card)
}


func newCard() string {
	
	return "Five of Diamonds"
}

 

 

Question 1:

If a function returns a value, do we have to annotate, or mark, the function with the type of value that is being returned?

Question 2:

The Go compiler is presenting an error message about the following function.  What should we do to fix the error?

func getSize() {
    return "30 meters"
}

 Question 3:

Is the following function valid?

func estimatePi() float64 {
    return 3.14
}

Question 4:

Is the following code valid?  Why or why not?

package main
 
import "fmt"
 
func main() {
    fmt.Println(getTitle())
}
 
func getTitle() string {
    return "Harry Potter"
}

 

Question 5:

Here's a tough one!  Imagine we have two files in the same folder with the same package name.  Will the following code successfully compile?  This might take a little experimentation on your side.  If you do try this out yourself, run your code with the command go run main.go state.go .

In main.go:

package main
 
func main() {
    printState()
}

 In a separate file called state.go:

package main
 
import "fmt"
 
func printState() {
    fmt.Println("California")
}

 

package main

import "fmt"

func main() {
	cards := []string{"Ace of Diamonds", newCard()}
	cards = append(cards, "Six of Spades")

	for i, card := range cards {

		fmt.Println(i, card)
	}
}

func newCard() string {

	return "Five of Diamonds"
}

 

 Test Your Knowledge: Slices and For Loops

Question 1:

Which of the following represents a slice where each element in it is of type int?

  • []string{}

  • []int{}

  • []float{}

 

 Question 2:

Is the following code valid?

colors := []strings{"Red", "Yellow", "Blue"} 

Question 3:

How do we iterate through each element in a slice and print out its value?

 Question 4:

Would the following code compile successfully?  Try it out yourself!  

 

for index, card := range cards {
    fmt.Println(card)
}


 

 Question 5:

Can a slice have both values of type 'int' and of type 'string' in it?

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值