Packages
Go programs consist of packages.
package main
Is executed from the main package
The package name is the last element name in the import path
import
Import other programs and libraries
Exported names
In Go, names that start with a capital letter are exported names that can be referenced from external packages. Names that start with a lowercase letter cannot be referenced by external packages.
exportnames.go
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.pi) //Cannot be referenced
fmt.Println(math.Pi) //Can be referenced
}
Functions
A function can take zero or more arguments. Write the type after the argument name and the return type after the argument.
func function name(Argument 1 Type of argument 1,・ ・ ・)Return type{
processing
}
function.go
func add(x int, y int) int {
return x + y
}
If two or more arguments are of the same type, the last type can be left and omitted.
function.go
func add(x, y int) int {
return x + y
}
The function can return multiple return values.
multiresult1.go
package main
import "fmt"
func swap(x, y string) (string, string) { //Return type()Defined in
return y, x
}
func main() {
a, b := swap("hello", "world")
fmt.Println(a, b)
}
Named return values
You can give a name to the variable that will be the return value. If you give a name to the return value, it will be treated as the variable name defined at the beginning of the function. You can use a named return variable to return without writing anything in the return statement.
multiresult2.go
package main
import "fmt"
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
func main() {
fmt.Println(split(17))
}
Variables
Declare the variable with the var statement. Declare the type after the last variable. Initial values can be defined for variables.
var1.go
package main
import "fmt"
var c, python, java bool
var k, j int = 1, 2
func main() {
var i int
fmt.Println(i, c, python, java)
fmt.Println(k,j)
}
In a function, you can make an implicit type declaration by using a short `` : introduced
assignment statement instead of a var declaration.
Outside the function, you need declarations that start with a keyword (var, func, etc.), and implicit declarations with ```: introduced` are not available.
in
var2.go
package main
import "fmt"
//Var cannot be omitted outside the function
//n := 10
func main() {
var i, j int = 1, 2
k := 3
c, python, java := true, false, "no!"
fmt.Println(i, j, k, c, python, java)
}
Type conversion in Go requires explicit conversion.
typeconversion.go
package main
import (
"fmt"
"math"
)
func main() {
var x, y int = 3, 4
var f float64 = math.Sqrt(float64(x*x + y*y))
var z uint = uint(f)
fmt.Println(x, y, z)
}
If you declare a variable without specifying an explicit type (either `: introduced` or`
var selected`), the type of the variable is type inferred from the variable on the right.
If the variable on the right has a type, the new variable on the left will have the same type.
If the number is untyped on the right, the new variable on the left will be of type int, float64, complex128 based on the precision of the constant on the right.
inference.go
package main
import "fmt"
func main() {
i := 42
fmt.Printf("i is of type %T\n", i)
f := 3.14
fmt.Printf("f is of type %T\n", f)
c := 0.867 + 0.5i
fmt.Printf("c is of type %T\n", c)
}
Constants are declared like variables using the const keyword. Constants can only be used with characters, strings, booleans, and numbers. Constants cannot be declared using ```: introduced`.
const.go
package main
import "fmt"
const Pi = 3.14
func main() {
const World = "world"
fmt.Println("Hello", World)
fmt.Println("Happy", Pi, "Day")
const Truth = true
fmt.Println("Go rules?", Truth)
}
Recommended Posts