go语言|数据结构:单链表(2)

简介: go语言|数据结构:单链表(2)

单向链表


  又称单链表,单链表中每个结点包含两部分,分别是数据域和指针域,上一个结点的指针指向下一结点,依次相连,形成链表。三个概念:首元结点、头结点和头指针,其中头结点在链表中不是必须的。

e1e6a7b420f644f4b75de59df622c4b0.png


首元结点

就是链表中存储第一个元素的结点。


头结点

是在首元结点之前附设的一个结点,其指针域指向首元结点。头结点的数据域可以存储链表的长度或者其它的信息,也可以为空不存储任何信息。


头指针

是指向链表中第一个结点的指针。若链表中有头结点,则头指针指向头结点;若链表中没有头结点,则头指针指向首元结点。


链表与节点

引入单链表结构LinkList,仅保留头指针;与节点结构ListNode配合使用。头指针为空的单链表即为空链表,解决只使用节点结构不能构造空表的缺点。

type ListNode struct {
  data int
  next *ListNode
}
type LinkList struct {
  next *ListNode
}



插入单个元素

头插法pushHead() 、 尾插法pushTail()

package main
import "fmt"
type ListNode struct {
  data int
  next *ListNode
}
type LinkList struct {
  next *ListNode
}
func (head *LinkList) pushHead(value int) {
  node := &ListNode{data: value}
  node.next = head.next
  head.next = node
}
func (head *LinkList) pushTail(value int) {
  node := &ListNode{data: value}
  p := head.next
  if p != nil {
    for p.next != nil {
      p = p.next
    }
    p.next = node
  } else {
    head.next = node
  }
}
func (node *ListNode) travel() {
  for node != nil {
    fmt.Print(node.data)
    node = node.next
    if node != nil {
      fmt.Print("->")
    }
  }
  fmt.Println("<nil>")
}
func (head *LinkList) travel() {
  head.next.travel()
}
func main() {
  nodes := &LinkList{}
  nodes.travel()
  nodes.pushTail(1)
  nodes.travel()
  nodes.pushHead(2)
  nodes.pushHead(3)
  nodes.travel()
  nodes.pushTail(4)
  nodes.pushTail(5)
  nodes.travel()
}
/*输出:
<nil>
1<nil>
3->2->1<nil>
3->2->1->4->5<nil>
*/


数组插入链表

优化后插入多个元素:push()前插、append()追加

package main
import "fmt"
type Node struct {
  data int
  next *Node
}
type List struct {
  next *Node
}
func (head *List) push(list []int) {
  for i := len(list) - 1; i >= 0; i-- {
    node := &Node{data: list[i]}
    node.next = head.next
    head.next = node
  }
}
func (head *List) append(list []int) {
  for i := 0; i < len(list); i++ {
    node := &Node{data: list[i]}
    p := head.next
    if p != nil {
      for p.next != nil {
        p = p.next
      }
      p.next = node
    } else {
      head.next = node
    }
  }
}
func (head *List) travel() {
  node := head.next
  for node != nil {
    fmt.Print(node.data)
    node = node.next
    if node != nil {
      fmt.Print("->")
    }
  }
  fmt.Println("<nil>")
}
func main() {
  node1 := &List{}
  node1.travel()
  node1.push([]int{1, 2, 3, 5})
  node1.travel()
  node1.append([]int{7, 8, 9})
  node1.travel()
  node1.push([]int{-2, -1, 0})
  node1.travel()
  node2 := &List{}
  node2.travel()
  node2.append([]int{1, 2, 3, 5})
  node2.travel()
  node2.push([]int{-2, -1, 0})
  node2.travel()
  node2.append([]int{7, 8, 9})
  node2.travel()
}
/*输出:
<nil>
1->2->3->5<nil>
1->2->3->5->7->8->9<nil>
-2->-1->0->1->2->3->5->7->8->9<nil>
<nil>
1->2->3->5<nil>
-2->-1->0->1->2->3->5<nil>
-2->-1->0->1->2->3->5->7->8->9<nil>
*/



链表长度

package main
import "fmt"
type Node struct {
  data int
  next *Node
}
type List struct {
  next *Node
}
func Len(head *List) int {
  length := 0
  for p := head.next; p != nil; p = p.next {
    length++
  }
  return length
}
func (head *List) size() int {
  return Len(head)
}
func (head *List) build(list []int) {
  for i := len(list) - 1; i >= 0; i-- {
    node := &Node{data: list[i]}
    node.next = head.next
    head.next = node
  }
}
func main() {
  var node1, node2 *List
  node1 = new(List)
  node2 = new(List)
  fmt.Println(node1.size())
  node1.build([]int{1, 2, 3, 5})
  node2.build([]int{7, 8, 9})
  fmt.Println(node1.size())
  fmt.Println(Len(node2))
}
/*输出:
0
4
3
*/


链表副本

package main
import "fmt"
type Node struct {
  data int
  next *Node
}
type List struct {
  next *Node
}
func (head *List) Copy() *List {
  p := head.next
  list := &List{}
  node := &Node{p.data, nil}
  tail := node
  for p = p.next; p != nil; p = p.next {
    var linknode = Node{data: p.data}
    (*tail).next = &linknode
    tail = &linknode
  }
  list.next = node
  return list
}
func (head *List) build(list []int) {
  for i := len(list) - 1; i >= 0; i-- {
    node := &Node{data: list[i]}
    node.next = head.next
    head.next = node
  }
}
func (head *List) pushHead(value int) {
  node := &Node{data: value}
  node.next = head.next
  head.next = node
}
func (head *List) travel() {
  p := head.next
  for p != nil {
    fmt.Print(p.data)
    p = p.next
    if p != nil {
      fmt.Print("->")
    }
  }
  fmt.Println("<nil>")
}
func main() {
  var node1, node2 *List
  node1 = new(List)
  node2 = new(List)
  node1.build([]int{1, 2, 3, 5})
  node2 = node1
  node3 := node1.Copy() //保存副本
  node3.travel()
  node1.pushHead(0)
  node1.travel()
  node2.travel()
  node3.travel() //保存的副本不随node1变化
}
/*输出:
1->2->3->5<nil>
0->1->2->3->5<nil>
0->1->2->3->5<nil>
1->2->3->5<nil>
*/



链表拼接


Cat()追加

与尾插单个元素类似;另外拼接链表的副本有好处的,不信可把.Copy()去掉试试。

package main
import "fmt"
type Node struct {
  data int
  next *Node
}
type List struct {
  next *Node
}
func (head *List) Cat(node *List) {
  p := head.next
  q := node.Copy() //使用副本,确保node不变
  if p != nil {
    for p.next != nil {
      p = p.next
    }
    p.next = q.next
  } else {
    head.next = q.next
  }
}
func (head *List) Copy() *List {
  p := head.next
  list := &List{}
  node := &Node{p.data, nil}
  tail := node
  for p = p.next; p != nil; p = p.next {
    var linknode = Node{data: p.data}
    (*tail).next = &linknode
    tail = &linknode
  }
  list.next = node
  return list
}
func (head *List) build(list []int) {
  for i := len(list) - 1; i >= 0; i-- {
    node := &Node{data: list[i]}
    node.next = head.next
    head.next = node
  }
}
func (head *List) pushHead(value int) {
  node := &Node{data: value}
  node.next = head.next
  head.next = node
}
func (head *List) pushTail(value int) {
  node := &Node{data: value}
  p := head.next
  if p != nil {
    for p.next != nil {
      p = p.next
    }
    p.next = node
  } else {
    head.next = node
  }
}
func (head *List) travel() {
  p := head.next
  for p != nil {
    fmt.Print(p.data)
    p = p.next
    if p != nil {
      fmt.Print("->")
    }
  }
  fmt.Println("<nil>")
}
func main() {
  var node1, node2 *List
  node1 = new(List)
  node2 = new(List)
  node1.build([]int{1, 2, 3, 5})
  node2.build([]int{7, 8, 9})
  node3 := &List{}
  node3.Cat(node1)
  node3.Cat(node2)
  node3.travel()
  node1.travel()
  node2.travel()
}
/*输出:
1->2->3->5->7->8->9<nil>
1->2->3->5<nil>
7->8->9<nil>
*/



Add()左加

package main
import "fmt"
type Node struct {
  data int
  next *Node
}
type List struct {
  next *Node
}
func (head *List) Add(node *List) {
  q := node.Copy()
  p := q.next
  for p.next != nil {
    p = p.next
  }
  p.next = head.next
  head.next = q.next
}
func (head *List) Cat(node *List) {
  p := head.next
  q := node.Copy()
  if p != nil {
    for p.next != nil {
      p = p.next
    }
    p.next = q.next
  } else {
    head.next = q.next
  }
}
func (head *List) Copy() *List {
  p := head.next
  list := &List{}
  node := &Node{p.data, nil}
  tail := node
  for p = p.next; p != nil; p = p.next {
    var linknode = Node{data: p.data}
    (*tail).next = &linknode
    tail = &linknode
  }
  list.next = node
  return list
}
func (head *List) build(list []int) {
  for i := len(list) - 1; i >= 0; i-- {
    node := &Node{data: list[i]}
    node.next = head.next
    head.next = node
  }
}
func (head *List) pushHead(value int) {
  node := &Node{data: value}
  node.next = head.next
  head.next = node
}
func (head *List) pushTail(value int) {
  node := &Node{data: value}
  p := head.next
  if p != nil {
    for p.next != nil {
      p = p.next
    }
    p.next = node
  } else {
    head.next = node
  }
}
func (head *List) travel() {
  p := head.next
  for p != nil {
    fmt.Print(p.data)
    p = p.next
    if p != nil {
      fmt.Print("->")
    }
  }
  fmt.Println("<nil>")
}
func main() {
  var node1, node2 *List
  node1 = new(List)
  node2 = new(List)
  node1.build([]int{1, 2, 3, 5})
  node2.build([]int{7, 8, 9})
  node3 := &List{}
  node3.Add(node1)
  node3.travel()
  node3.Add(node2)
  node3.travel()
  node3.Add(node1)
  node3.travel()
  node3.Cat(node2)
  node3.travel()
  node1.travel()
  node2.travel()
}
/*输出:
1->2->3->5<nil>
7->8->9->1->2->3->5<nil>
1->2->3->5->7->8->9->1->2->3->5<nil>
1->2->3->5->7->8->9->1->2->3->5->7->8->9<nil>
1->2->3->5<nil>
7->8->9<nil>
*/


节点删除

节点删除需要判断链表自身是否为空链表,判断条件增加 if head.next == nil || p.next == nil {...}


删除首元结点

package main
import "fmt"
type Node struct {
  data int
  next *Node
}
type List struct {
  next *Node
}
func (head *List) delHead() {
  p := head.next
  if head.next == nil || p.next == nil {
    head.next = nil
  } else {
    head.next = p.next
  }
}
func (head *List) build(list []int) {
  for i := len(list) - 1; i >= 0; i-- {
    node := &Node{data: list[i]}
    node.next = head.next
    head.next = node
  }
}
func (head *List) travel() {
  for p := head.next; p != nil; p = p.next {
    fmt.Print(p.data)
    if p.next != nil {
      fmt.Print("->")
    }
  }
  fmt.Println("<nil>")
}
func main() {
  nodes := &List{}
  nodes.build([]int{1, 2, 3})
  nodes.travel()
  nodes.delHead()
  nodes.travel()
  nodes.delHead()
  nodes.travel()
  nodes.delHead()
  nodes.travel()
  nodes.delHead()
  nodes.travel()
}
/*输出:
1->2->3<nil>
2->3<nil>
3<nil>
<nil>
<nil>
*/


删除尾结点

package main
import "fmt"
type Node struct {
  data int
  next *Node
}
type List struct {
  next *Node
}
func (head *List) delTail() {
  p := head.next
  if head.next == nil || p.next == nil {
    head.next = nil
  } else {
    for p.next.next != nil {
      p = p.next
    }
    p.next = nil
  }
}
func (head *List) build(list []int) {
  for i := len(list) - 1; i >= 0; i-- {
    node := &Node{data: list[i]}
    node.next = head.next
    head.next = node
  }
}
func (head *List) travel() {
  for p := head.next; p != nil; p = p.next {
    fmt.Print(p.data)
    if p.next != nil {
      fmt.Print("->")
    }
  }
  fmt.Println("<nil>")
}
func main() {
  nodes := &List{}
  nodes.build([]int{1, 2, 3})
  nodes.travel()
  nodes.delTail()
  nodes.travel()
  nodes.delTail()
  nodes.travel()
  nodes.delTail()
  nodes.travel()
  nodes.delTail()
  nodes.travel()
}
/*输出:
1->2->3<nil>
1->2<nil>
1<nil>
<nil>
<nil>
*/


习题解答


1. 给定一个已排序链表,删除重复节点,原始链表中多次出现的数字只能保留一次。


示例1

输入: 1->1->2

输出: 1->2

示例2

输入: 1->1->2->3->3

输出: 1->2->3


package main
import "fmt"
type Node struct {
  data int
  next *Node
}
type List struct {
  next *Node
}
func (head *List) removeDuplicates() {
  p := head.next
  node := &List{}
  data := p.data
  node.pushTail(data)
  for p != nil {
    if p.data != data {
      data = p.data
      node.pushTail(data)
    }
    p = p.next
  }
  head.next = node.next
}
func (head *List) pushTail(value int) {
  node := &Node{data: value}
  p := head.next
  if p != nil {
    for p.next != nil {
      p = p.next
    }
    p.next = node
  } else {
    head.next = node
  }
}
func (head *List) build(list []int) {
  for i := len(list) - 1; i >= 0; i-- {
    node := &Node{data: list[i]}
    node.next = head.next
    head.next = node
  }
}
func (head *List) clear() {
  head.next = nil
}
func (head *List) travel() {
  for p := head.next; p != nil; p = p.next {
    fmt.Print(p.data)
    if p.next != nil {
      fmt.Print("->")
    }
  }
  fmt.Println()
  //fmt.Println("<nil>")
}
func main() {
  nodes := &List{}
  nodes.build([]int{1, 1, 2})
  nodes.travel()
  nodes.removeDuplicates()
  nodes.travel()
  nodes.clear()
  nodes.build([]int{1, 1, 2, 3, 3})
  nodes.travel()
  nodes.removeDuplicates()
  nodes.travel()
  nodes.clear()
  nodes.build([]int{1, 2, 3, 3, 4, 4, 5})
  nodes.travel()
  nodes.removeDuplicates()
  nodes.travel()
  nodes.clear()
  nodes.build([]int{1, 1, 1, 2, 3, 3, 3})
  nodes.travel()
  nodes.removeDuplicates()
  nodes.travel()
}
/*输出:
1->1->2
1->2
1->1->2->3->3
1->2->3
1->2->3->3->4->4->5
1->2->3->4->5
1->1->1->2->3->3->3
1->2->3
*/


2. 给定一个排序链表,删除所有重复的节点,留原始链表有过重复的数字一个也不留。

示例1

输入: 1->2->3->3->4->4->5

输出: 1->2->5

示例2

输入: 1->1->1->2->3

输出: 2->3

package main
import "fmt"
type Node struct {
  data int
  next *Node
}
type List struct {
  next *Node
}
func (head *List) deleteDuplicates() {
  p := head.next
  node := &List{}
  if p.next == nil || p.data != p.next.data {
    node.pushTail(p.data)
  }
  if p.next != nil {
    data := p.data
    for p.next.next != nil {
      data = p.data
      p = p.next
      if data != p.data && p.data != p.next.data {
        node.pushTail(p.data)
      }
    }
    if p.data != p.next.data {
      node.pushTail(p.next.data)
    }
  }
  head.next = node.next
}
func (head *List) pushTail(value int) {
  node := &Node{data: value}
  p := head.next
  if p != nil {
    for p.next != nil {
      p = p.next
    }
    p.next = node
  } else {
    head.next = node
  }
}
func (head *List) build(list []int) {
  for i := len(list) - 1; i >= 0; i-- {
    node := &Node{data: list[i]}
    node.next = head.next
    head.next = node
  }
}
func (head *List) clear() {
  head.next = nil
}
func (head *List) travel() {
  for p := head.next; p != nil; p = p.next {
    fmt.Print(p.data)
    if p.next != nil {
      fmt.Print("->")
    }
  }
  fmt.Println("")
  //fmt.Println("<nil>")
}
func main() {
  nodes := &List{}
  nodes.build([]int{1, 1, 1, 2, 3})
  nodes.travel()
  nodes.deleteDuplicates()
  nodes.travel()
  nodes.clear()
  nodes.build([]int{1, 2, 3, 3, 4, 4, 5})
  nodes.travel()
  nodes.deleteDuplicates()
  nodes.travel()
  nodes.clear()
  nodes.build([]int{1, 1, 2, 3, 3, 4, 5})
  nodes.travel()
  nodes.deleteDuplicates()
  nodes.travel()
  nodes.clear()
  nodes.build([]int{1, 2, 3, 3, 4, 5, 5})
  nodes.travel()
  nodes.deleteDuplicates()
  nodes.travel()
}
/*输出:
1->1->1->2->3
2->3
1->2->3->3->4->4->5
1->2->5
1->1->2->3->3->4->5
2->4->5
1->2->3->3->4->5->5
1->2->4
*/

3. 给定两个有序链表(升序),合并为一个新的有序链表并返回。

   示例1

   输入:1->2>4->8

      1->3->3->5->5

   输出:1->1->2->3->3->4->5->5->8

   示例2

   输入:0->2>4->8

      1->3->5->7->9

   输出:0->1->2->3->4->5->6->7->8->9


1、递归法:

package main
import "fmt"
type Node struct {
  data int
  next *Node
}
type List struct {
  head *Node
}
//递归法合并有序链表
func recurMerge(p1 *Node, p2 *Node) *Node {
  if p1 == nil {
    return p2
  }
  if p2 == nil {
    return p1
  }
  p := new(Node)
  if p1.data < p2.data {
    p = p1
    p.next = recurMerge(p1.next, p2)
  } else {
    p = p2
    p.next = recurMerge(p1, p2.next)
  }
  return p
}
func (list *List) build(lst []int) {
  for i := len(lst) - 1; i >= 0; i-- {
    node := &Node{data: lst[i]}
    node.next = list.head
    list.head = node
  }
}
func (list *List) Copy() *List {
  p := list.head
  res := &List{}
  if p != nil {
    node := &Node{p.data, nil}
    q := node
    for p = p.next; p != nil; p = p.next {
      q.next = &Node{p.data, nil}
      q = q.next
    }
    res.head = node
  }
  return res
}
func (list *List) travel() {
  for p := list.head; p != nil; p = p.next {
    fmt.Print(p.data)
    if p.next != nil {
      fmt.Print("->")
    }
  }
  fmt.Println("<nil>")
}
func main() {
  List1 := &List{}
  List2 := &List{}
  List1.build([]int{1, 2, 4, 8})
  List2.build([]int{1, 3, 3, 5, 5})
  node1 := List1.Copy().head
  node2 := List2.Copy().head
  List0 := &List{}
  List0.head = recurMerge(node1, node2)
  List0.travel()
  List1.travel()
  List2.travel()
  //不使用副本的对比
  node1 = List1.head
  node2 = List2.head
  List0 = &List{}
  List0.head = recurMerge(node1, node2)
  List0.travel()
  List1.travel()
  List2.travel()
  //另一组合并
  List1 = &List{}
  List2 = &List{}
  List1.build([]int{0, 2, 4, 8})
  List2.build([]int{1, 3, 5, 7, 9})
  node1 = List1.Copy().head
  node2 = List2.Copy().head
  List0 = &List{}
  List0.head = recurMerge(node1, node2)
  List0.travel()
  List1.travel()
  List2.travel()
}
/*输出:
1->1->2->3->3->4->5->5->8<nil>
1->2->4->8<nil>
1->3->3->5->5<nil>
1->1->2->3->3->4->5->5->8<nil>
1->2->3->3->4->5->5->8<nil>
1->1->2->3->3->4->5->5->8<nil>
0->1->2->3->4->5->7->8->9<nil>
0->2->4->8<nil>
1->3->5->7->9<nil>
*/



本例中链表类的next指针改名为head:

type List struct {
    head *Node
} 


2、常规遍历:

1.


package main
import "fmt"
type Node struct {
  data int
  next *Node
}
type List struct {
  head *Node
}
func mergeLists(list1 *List, list2 *List) *List {
  list := &List{&Node{}} //初始赋值时多余一个默认值0
  p := list.head
  p1 := list1.Copy().head  //使用副本保留链表原样
  p2 := list2.Copy().head
  for ; p1 != nil && p2 != nil; p = p.next {
    if p1.data < p2.data {
      p.next = p1
      p1 = p1.next
    } else {
      p.next = p2
      p2 = p2.next
    }
  }
  if p1 == nil {
    p.next = p2
  } else if p2 == nil {
    p.next = p1
  }
  list.head = list.head.next //去掉初始化时的0值
  return list
}
func (list *List) build(lst []int) {
  for i := len(lst) - 1; i >= 0; i-- {
    node := &Node{data: lst[i]}
    node.next = list.head
    list.head = node
  }
}
func (list *List) Copy() *List {
  p := list.head
  res := &List{}
  if p != nil {
    node := &Node{p.data, nil}
    q := node
    for p = p.next; p != nil; p = p.next {
      q.next = &Node{p.data, nil}
      q = q.next
    }
    res.head = node
  }
  return res
}
func (list *List) travel() {
  for p := list.head; p != nil; p = p.next {
    fmt.Print(p.data)
    if p.next != nil {
      fmt.Print("->")
    }
  }
  fmt.Println("<nil>")
}
func main() {
  List1 := &List{}
  List2 := &List{}
  List1.build([]int{1, 2, 4, 8})
  List2.build([]int{1, 3, 3, 5, 5})
  List0 := mergeLists(List1, List2)
  List0.travel()
  List1.travel()
  List2.travel()
  List1 = &List{}
  List2 = &List{}
  List1.build([]int{0, 2, 4, 8})
  List2.build([]int{1, 3, 5, 7, 9})
  List0 = mergeLists(List1, List2)
  List0.travel()
  List1.travel()
  List2.travel()
}
/*输出:
1->1->2->3->3->4->5->5->8<nil>
1->2->4->8<nil>
1->3->3->5->5<nil>
0->1->2->3->4->5->7->8->9<nil>
0->2->4->8<nil>
1->3->5->7->9<nil>
*/


2e5a4dee1e3d422387f478ad5b1a29b2.png


目录
相关文章
|
4天前
|
数据采集 存储 Go
使用Go语言和chromedp库下载Instagram图片:简易指南
Go语言爬虫示例使用chromedp库下载Instagram图片,关键步骤包括设置代理IP、创建带代理的浏览器上下文及执行任务,如导航至用户页面、截图并存储图片。代码中新增`analyzeAndStoreImage`函数对图片进行分析和分类后存储。注意Instagram的反爬策略可能需要代码适时调整。
使用Go语言和chromedp库下载Instagram图片:简易指南
|
1天前
|
人工智能 Go 调度
掌握Go并发:Go语言并发编程深度解析
掌握Go并发:Go语言并发编程深度解析
|
1天前
|
SQL 关系型数据库 MySQL
Golang数据库编程详解 | 深入浅出Go语言原生数据库编程
Golang数据库编程详解 | 深入浅出Go语言原生数据库编程
|
1天前
|
Go 开发者
Golang深入浅出之-Go语言流程控制:if、switch、for循环详解
【4月更文挑战第21天】本文介绍了Go语言中的流程控制语句,包括`if`、`switch`和`for`循环。`if`语句支持简洁的语法和初始化语句,但需注意比较运算符的使用。`switch`语句提供多分支匹配,可省略`break`,同时支持不带表达式的形式。`for`循环有多种形式,如基本循环和`for-range`遍历,遍历时修改原集合可能导致未定义行为。理解并避免易错点能提高代码质量和稳定性。通过实践代码示例,可以更好地掌握Go语言的流程控制。
11 3
Golang深入浅出之-Go语言流程控制:if、switch、for循环详解
|
1天前
|
Go
Golang深入浅出之-Go语言函数基础:定义、调用与多返回值
【4月更文挑战第21天】Go语言函数是代码组织的基本单元,用于封装可重用逻辑。本文介绍了函数定义(包括基本形式、命名、参数列表和多返回值)、调用以及匿名函数与闭包。在函数定义时,注意参数命名和注释,避免参数顺序混淆。在调用时,要检查并处理多返回值中的错误。理解闭包原理,小心处理外部变量引用,以提升代码质量和可维护性。通过实践和示例,能更好地掌握Go语言函数。
15 1
Golang深入浅出之-Go语言函数基础:定义、调用与多返回值
|
2天前
|
程序员 Go API
【Go语言快速上手(二)】 分支与循环&函数讲解
【Go语言快速上手(二)】 分支与循环&函数讲解
|
3天前
|
Go
Golang深入浅出之-Go语言基础语法:变量声明与赋值
【4月更文挑战第20天】本文介绍了Go语言中变量声明与赋值的基础知识,包括使用`var`关键字和简短声明`:=`的方式,以及多变量声明与赋值。强调了变量作用域、遮蔽、初始化与零值的重要性,并提醒读者注意类型推断时的一致性。了解这些概念有助于避免常见错误,提高编程技能和面试表现。
18 0
|
3天前
|
编译器 Go 开发者
Go语言入门|包、关键字和标识符
Go语言入门|包、关键字和标识符
22 0
|
6天前
|
API Go
使用Go语言通过API获取代理IP并使用获取到的代理IP
使用Go语言通过API获取代理IP并使用获取到的代理IP
|
7天前
|
前端开发 Java Go
开发语言详解(python、java、Go(Golong)。。。。)
开发语言详解(python、java、Go(Golong)。。。。)