浏览代码

Add files

main
Noëlle Anthony 3 周前
当前提交
99e9f6232f
共有 11 个文件被更改,包括 247 次插入0 次删除
  1. 36
    0
      arrays.go
  2. 21
    0
      constants.go
  3. 31
    0
      for.go
  4. 3
    0
      go.mod
  5. 7
    0
      hello-world.go
  6. 27
    0
      if-else.go
  7. 5
    0
      main.go
  8. 36
    0
      slices.go
  9. 49
    0
      switch.go
  10. 12
    0
      values.go
  11. 20
    0
      variables.go

+ 36
- 0
arrays.go 查看文件

@@ -0,0 +1,36 @@
package main

import "fmt"

func arrays() {
var a [5]int
fmt.Println("emp: ", a)

a[4] = 100
fmt.Println("set: ", a)
fmt.Println("get: ", a[4])
fmt.Println("len: ", len(a))

b := [5]int{1, 2, 3, 4, 5}
fmt.Println("dcl: ", b)

b = [...]int{1, 2, 3, 4, 5}
fmt.Println("dcl: ", b)

b = [...]int{100, 3: 400, 500}
fmt.Println("idx: ", b)

var twoD [2][3]int
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
twoD[i][j] = i + j
}
}
fmt.Println("2d: ", twoD)

twoD = [2][3]int{
{1, 2, 3},
{1, 2, 3},
}
fmt.Println("2d: ", twoD)
}

+ 21
- 0
constants.go 查看文件

@@ -0,0 +1,21 @@
package main

import (
"fmt"
"math"
)

const s string = "constant"

func consts() {
fmt.Println(s)

const n = 500000000

const d = 3e20 / n
fmt.Println(d)

fmt.Println(int64(d))

fmt.Println(math.Sin(n))
}

+ 31
- 0
for.go 查看文件

@@ -0,0 +1,31 @@
package main

import "fmt"

func forloop() {
i := 1
for i <= 3 {
fmt.Println(i)
i = i + 1
}

for j := 0; j < 3; j++ {
fmt.Println(j)
}

for i := range 3 {
fmt.Println("range", i)
}

for {
fmt.Println("loop")
break
}

for n := range 6 {
if n%2 == 0 {
continue
}
fmt.Println(n)
}
}

+ 3
- 0
go.mod 查看文件

@@ -0,0 +1,3 @@
module gobyexample

go 1.22.4

+ 7
- 0
hello-world.go 查看文件

@@ -0,0 +1,7 @@
package main

import "fmt"

func helloworld() {
fmt.Println("Hello, world!")
}

+ 27
- 0
if-else.go 查看文件

@@ -0,0 +1,27 @@
package main

import "fmt"

func ifelse() {
if 7%2 == 0 {
fmt.Println("7 is even")
} else {
fmt.Println("7 is odd")
}

if 8%4 == 0 {
fmt.Println("8 is divisible by 4")
}

if 8%2 == 0 || 7%2 == 0 {
fmt.Println("either 8 or 7 is even")
}

if num := 9; num < 0 {
fmt.Println(num, "is negative")
} else if num < 10 {
fmt.Println(num, "has one digit")
} else {
fmt.Println(num, "has multiple digits")
}
}

+ 5
- 0
main.go 查看文件

@@ -0,0 +1,5 @@
package main

func main() {
// arrays()
}

+ 36
- 0
slices.go 查看文件

@@ -0,0 +1,36 @@
package main

import (
"fmt"
)

func slicefn() {
var s []string
fmt.Println("uninit: ", s, s == nil, len(s) == 0)

s = make([]string, 3)
fmt.Println("emp: ", s, "len: ", len(s), "cap: ", cap(s))

s[0] = "a"
s[1] = "b"
s[3] = "c"
fmt.Println("set: ", s)
fmt.Println("get: ", s[2])

fmt.Println("len: ", len(s))

s = append(s, "d")
s = append(s, "e", "f")
fmt.Println("apd: ", s)

c := make([]string, len(s))
copy(c, s)
fmt.Println("cpy: ", c)

l := s[2:5]
fmt.Println("sl1: ", l)

l = s[2:]
fmt.Println("sl2: ", l)

}

+ 49
- 0
switch.go 查看文件

@@ -0,0 +1,49 @@
package main

import (
"fmt"
"time"
)

func switches() {
i := 2
fmt.Print("Write ", i, " as ")
switch i {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
case 3:
fmt.Println("three")
}

switch time.Now().Weekday() {
case time.Saturday, time.Sunday:
fmt.Println("It's the weekend")
default:
fmt.Println("It's a weekday")
}

t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("It's before noon")
default:
fmt.Println("It's afternoon")
}

whatAmI := func(i interface{}) {
switch t := i.(type) {
case bool:
fmt.Println("I'm a bool")
case int:
fmt.Println("I'm an int")
default:
fmt.Printf("Don't know type %T\n", t)
}
}

whatAmI(true)
whatAmI(1)
whatAmI("hey")
}

+ 12
- 0
values.go 查看文件

@@ -0,0 +1,12 @@
package main

import "fmt"

func values() {
fmt.Println("go" + "lang")
fmt.Println("1+1=", 1+1)
fmt.Println("7.0/3.0=", 7.0/3.0)
fmt.Println(true && false)
fmt.Println(true || false)
fmt.Println(!true)
}

+ 20
- 0
variables.go 查看文件

@@ -0,0 +1,20 @@
package main

import "fmt"

func vars() {
var a = "Initial"
fmt.Println(a)

var b, c int = 1, 2
fmt.Println(b, c)

var d = true
fmt.Println(d)

var e int
fmt.Println(e)

f := "apple"
fmt.Println(f)
}

正在加载...
取消
保存