import “./model” //当前文件同一目录的model目录,但是不建议这种方式来import
import “shorturl/model” //加载gopath/src/shorturl/model模块
上面展示了一些import常用的几种方式,但是还有一些特殊的import,让很多新手很费解,下面我们来一一讲解一下 到底是怎么一回事
我们有时候会看到如下的方式导入包
import(
. "fmt"
)
这个点操作的含义就是这个包导入之后在你调用这个包的函数时,你可以省略前缀的包名,也就是前面你调 用的fmt.Println(“hello world”)可以省略的写成Println(“hello world”)
别名操作顾名思义我们可以把包命名成另一个我们用起来容易记忆的名字
import(
f "fmt"
)
别名操作的话调用包函数时前缀变成了我们的前缀,即f.Println(“hello world”)
这个操作经常是让很多人费解的一个操作符,请看下面这个import
import (
"database/sql"
_ "github.com/ziutek/mymysql/godrv"
)
_操作其实是引入该包,而不直接使用包里面的函数,而是调用了该包里面的init函数。
package main
import (
"fmt"
"log"
"os"
"github.com/fsnotify"
)
var watcher *fsnotify.Watcher
func watchDir(path string) {
fmt.Println("watchDir:", path)
err := watcher.Add(path)
if err != nil {
fmt.Println("watcher.Add failed", err.Error())
return
}
}
func isDir(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
fmt.Println("os.Stat failed", err.Error())
return false
}
return fileInfo.IsDir()
}
func browserDir(path string) {
fmt.Println("browserDir:", path)
dir, err := os.Open(path)
if err != nil {
fmt.Println("os.Open failed", err.Error())
return
}
defer dir.Close()
watchDir(path)
names, err := dir.Readdirnames(-1)
if err != nil {
fmt.Println("dir.Readdirnames failed", err.Error())
return
}
for _, name := range names {
sub := path + "\\" + name
if !isDir(sub) {
continue
}
browserDir(sub)
}
}
func main() {
var err error
watcher, err = fsnotify.NewWatcher()
if err != nil {
fmt.Println("fsnotify.NewWatcher failed", err.Error())
return
}
defer watcher.Close()
browserDir("E:\\fsnotify_demo")
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
log.Println("event:", event)
if event.Op&fsnotify.Create == fsnotify.Create {
log.Println("Create file:", event.Name)
}
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("Write file:", event.Name)
}
if event.Op&fsnotify.Remove == fsnotify.Remove {
log.Println("Remove file:", event.Name)
}
if event.Op&fsnotify.Rename == fsnotify.Rename {
log.Println("Rename file:", event.Name)
}
if event.Op&fsnotify.Chmod == fsnotify.Chmod {
log.Println("Chmod file:", event.Name)
}
case err := <-watcher.Errors:
log.Println("error:", err)
}
}
}()
<-done
}
https://leetcode.com/problems/count-and-say/description/
The count-and-say sequence is the sequence of integers with the first five terms as following:
1. 1
2. 11
3. 21
4. 1211
5. 111221
1
is read off as "one 1"
or 11
.
11
is read off as "two 1s"
or 21
.
21
is read off as "one 2
, then one 1"
or 1211
.
Given an integer n, generate the n^th term of the count-and-say sequence.
Note: Each term of the sequence of integers will be represented as a string.
Example 1:
Input: 1
Output: "1"
Example 2:
Input: 4
Output: "1211"
package main
import (
"fmt"
"strconv"
)
func nextSequence(now string) string {
b := []byte(now)
fmt.Println(now)
fmt.Println(b)
for i := 0; i < len(b); i++ {
fmt.Printf("%c ", b[i])
}
fmt.Println("*********")
nextSeq := ""
curCharacter := b[0]
curNum := 1
for i := 1; i < len(b); i++ {
if b[i] == curCharacter {
curNum++
} else {
strCurNum := strconv.Itoa(curNum)
nextSeq = nextSeq + strCurNum
nextSeq = fmt.Sprintf("%s%c", nextSeq, curCharacter)
curCharacter = b[i]
curNum = 1
}
}
strCurNum := strconv.Itoa(curNum)
nextSeq = nextSeq + strCurNum
nextSeq = fmt.Sprintf("%s%c", nextSeq, curCharacter)
return nextSeq
}
func countAndSay(n int) string {
now := "1"
for i := 1; i < n; i++ {
now = nextSequence(now)
}
return now
}
func main() {
fmt.Println("main", countAndSay(4))
}
https://leetcode.com/problems/search-insert-position/description/
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Example 1:
Input: [1,3,5,6], 5
Output: 2
Example 2:
Input: [1,3,5,6], 2
Output: 1
Example 3:
Input: [1,3,5,6], 7
Output: 4
Example 4:
Input: [1,3,5,6], 0
Output: 0
package main
import "fmt"
func searchInsert(nums []int, target int) int {
fmt.Println(len(nums))
fmt.Println(nums)
fmt.Println(target)
if len(nums) == 0 {
return 0
}
if target < nums[0] {
return 0
}
if target > nums[len(nums)-1] {
return len(nums)
}
start := 0
end := len(nums) - 1
for start <= end {
fmt.Println("start", "end", start, end)
if target < nums[start] {
return start
}
if target > nums[end] {
return end + 1
}
mid := (start + end) / 2
if target == nums[mid] {
return mid
} else if target < nums[mid] {
end = mid - 1
} else {
start = mid + 1
}
}
return 0
}
func main() {
fmt.Println(searchInsert([]int{1, 3, 5, 6}, 7))
}
https://leetcode.com/problems/single-number/description/
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
package main
import "fmt"
func singleNumber(nums []int) int {
result := 0
for _, num := range nums {
result = result ^ num
}
return result
}
func main() {
fmt.Println(singleNumber([]int{1, 1, 2, 3, 3}))
}
https://projecteuler.net/problem=5
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
232792560
package main
import (
"fmt"
)
func bIsPrimer(num int) bool {
for i := 2; i < num; i++ {
if num%i == 0 {
return false
}
}
return true
}
func getFactor(num int) []int {
var s []int
i := 2
for {
if i >= num {
break
}
if num%i == 0 {
s = append(s, i)
for num > 0 && num%i == 0 {
num /= i
}
i++
continue
} else {
i++
}
}
return s
}
func main() {
result := 2
for i := 3; i <= 20; i++ {
if bIsPrimer(i) {
result = result * i
} else if result%i == 0 {
continue
} else {
s := getFactor(i)
for _, v := range s {
result = result * v
}
}
}
fmt.Println(result)
}